Xamarin Forms SwipeView Content Label

Joey Ireland 1 Reputation point
2021-03-08T11:18:49.997+00:00

Hi All,

I have a SwipeView in a ListView and I want to get the label text when I swipe and hit the delete button. my SwipeView looks like this but I cant see a way in the code to get the ID as when using the x:Name it does not exist inside the delete invoke method.

<ListView x:Name="RAMsListView" RowHeight="75" HasUnevenRows="False" ItemSelected="RAMsList_ItemSelected" Margin="0,20,0,0" SeparatorVisibility="None">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <SwipeView x:Name="HazardSwipeLayout" BackgroundColor="White">
                                <SwipeView.RightItems>
                                    <SwipeItems>
                                        <SwipeItem
                                            IconImageSource="binicon.png"
                                            BackgroundColor="Red"
                                            Invoked="SwipeItem_Invoked" />
                                        <SwipeItem
                                            Text="Duplicate RAMs"
                                            BackgroundColor="SkyBlue"
                                            Invoked="SwipeItem_Invoked_1" CommandParameter="{Binding Id}" />
                                    </SwipeItems>
                                </SwipeView.RightItems>
                                <!-- Content -->
                                <Frame BackgroundColor="White" HasShadow="False" CornerRadius="10" Margin="0,5,0,5">
                                    <Grid ColumnDefinitions="40,300,150,*">
                                        <Image Source="timesheets.png" Grid.Column="0" Grid.Row="0" HeightRequest="30" WidthRequest="30" HorizontalOptions="Start"/>
                                        <Label Text="{Binding ProjectName}" TextColor="Black" FontSize="16" Grid.Column="1" VerticalTextAlignment="Center" HorizontalTextAlignment="Start" />
                                        <Label Text="{Binding StartDate, StringFormat='{0:dd/MM/yyyy}'}" TextColor="Black" FontSize="16" Grid.Column="3" VerticalTextAlignment="Center" HorizontalTextAlignment="End" />
                                        <Label Text="{Binding Id}" x:Name="RamId" IsVisible="False" Grid.Column="2" />
                                    </Grid>
                                </Frame>
                            </SwipeView>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2021-03-08T12:35:57.257+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can use Command to get the value of current item., I add test command to the first <SwipeItem>. And I use data-binding to achieve the demo code.

    Firstly, I notice you have three properties. So I create the following model called RAM.cs.

       public class RAM  
           {  
               public string ProjectName { get; set; }  
         
               public DateTime StartDate { get; set; }  
               public int Id { get; set; }  
           }  
    

    Then I change following layout XAML. I add ItemsSource with databinding, add Command to the first SwipeItem and delete Invoked method and CommandParameter="{Binding Id}"

       <ListView ItemsSource="{Binding RAMs}" x:Name="RAMsListView" RowHeight="75" HasUnevenRows="False" ItemSelected="RAMsListView_ItemSelected" Margin="0,20,0,0" SeparatorVisibility="None">  
                   <ListView.ItemTemplate>  
                       <DataTemplate>  
                           <ViewCell>  
                               <SwipeView x:Name="HazardSwipeLayout" BackgroundColor="White">  
                                   <SwipeView.RightItems>  
                                       <SwipeItems>  
                                           <SwipeItem  
                                                    Command="{Binding BindingContext.DeleteCommand, Source={x:Reference Name=RAMsListView}}"  
                                                    CommandParameter="{Binding .}"  
                                                    IconImageSource="binicon.png"  
                                                    BackgroundColor="Red"  
                                                    />  
                                           <SwipeItem  
                                                    Text="Duplicate RAMs"  
                                                    BackgroundColor="SkyBlue"  
                                                    />  
                                       </SwipeItems>  
                                   </SwipeView.RightItems>  
                                   <!-- Content -->  
                                   <Frame BackgroundColor="White" HasShadow="False" CornerRadius="10" Margin="0,5,0,5">  
                                       <Grid ColumnDefinitions="40,300,150,*">  
                                           <Image Source="timesheets.png" Grid.Column="0" Grid.Row="0" HeightRequest="30" WidthRequest="30" HorizontalOptions="Start"/>  
                                           <Label Text="{Binding ProjectName}" TextColor="Black" FontSize="16" Grid.Column="1" VerticalTextAlignment="Center" HorizontalTextAlignment="Start" />  
                                           <Label Text="{Binding StartDate}" TextColor="Black" FontSize="16" Grid.Column="3" VerticalTextAlignment="Center" HorizontalTextAlignment="End" />  
                                           <Label Text="{Binding Id}" x:Name="RamId" IsVisible="False" Grid.Column="2" />  
                                       </Grid>  
                                   </Frame>  
                               </SwipeView>  
                           </ViewCell>  
                       </DataTemplate>  
                   </ListView.ItemTemplate>  
               </ListView>  
    

    In the layout's background code. If click the first SwipeItem, We can get current item's all information(id,StartDate,ProjectName ), I make a test that I guess you want to get ID, so I make a displayAlert to show it.

       using System;  
       using System.Collections.Generic;  
       using System.ComponentModel;  
       using System.Linq;  
       using System.Text;  
       using System.Threading.Tasks;  
       using System.Windows.Input;  
       using Xamarin.Forms;  
         
       namespace App61  
       {  
           public partial class MainPage : ContentPage  
           {  
               public List<RAM> RAMs { get; set; }  
               public ICommand DeleteCommand { protected set; get; }  
               public MainPage()  
               {  
                   RAMs = new List<RAM>();  
                   RAMs.Add(new RAM() { Id=1, ProjectName="project1", StartDate=DateTime.Now });  
                   RAMs.Add(new RAM() { Id = 2, ProjectName = "project2", StartDate = DateTime.Now });  
                   RAMs.Add(new RAM() { Id = 3, ProjectName = "project3", StartDate = DateTime.Now });  
         
         
                   DeleteCommand = new Command<RAM>(async (key) => {  
                       RAM SelectRam = key as RAM;  
                       await DisplayAlert("info", SelectRam.Id.ToString(),"OK");  
         
                      
         
                   });  
         
                   BindingContext = this;  
                   InitializeComponent();  
               }  
         
               private void RAMsListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)  
               {  
         
               }  
         
               private void SwipeItem_Invoked(object sender, EventArgs e)  
               {  
         
               }  
         
               private void SwipeItem_Invoked_1(object sender, EventArgs e)  
               {  
         
               }  
           }  
         
           public class RAM  
           {  
               public string ProjectName { get; set; }  
         
               public DateTime StartDate { get; set; }  
               public int Id { get; set; }  
           }  
       }  
    

    Here is running screenshot when I click first item's <SwipeItem>.

    75434-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments