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>
.
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.