From Xamarin.Forms SwipeView, we can see that SwipeStarted is fired when a swipe starts. The SwipeStartedEventArgs object that accompanies this event has a SwipeDirection property, of type SwipeDirection.
So you can get current listview item from OnDeleteSwipeItemInvoked method.
I do one sample that you can take a look:
<ListView ItemsSource="{Binding items}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItemView Invoked="SwipeItemView_Invoked">
<StackLayout
BackgroundColor="Red"
Orientation="Vertical"
WidthRequest="200">
<StackLayout HorizontalOptions="Start">
<Image
Margin="70,15,0,0"
HeightRequest="25"
Source="delete.png"
WidthRequest="25" />
<Label
Margin="70,0,0,0"
FontSize="15"
HorizontalOptions="CenterAndExpand"
Text=" Delete"
TextColor="White" />
</StackLayout>
</StackLayout>
</SwipeItemView>
</SwipeItems>
</SwipeView.RightItems>
<!-- Content -->
<SwipeView.Content>
<Grid
BackgroundColor="LightGray"
HeightRequest="60"
WidthRequest="300">
<Label
HorizontalOptions="Center"
Text="{Binding title}"
VerticalOptions="Center" />
</Grid>
</SwipeView.Content>
</SwipeView>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public partial class Page4 : ContentPage
{
public ObservableCollection<menuitem> items { get; set; }
public Page4()
{
InitializeComponent();
items = new ObservableCollection<menuitem>()
{
new menuitem(){title="title 1"},
new menuitem(){title="title 2"},
new menuitem(){title="title 3"},
new menuitem(){title="title 4"},
new menuitem(){title="title 5"},
new menuitem(){title="title 6"}
};
this.BindingContext = this;
}
private void SwipeItemView_Invoked(object sender, EventArgs e)
{
SwipeItemView item = sender as SwipeItemView;
menuitem model = item.BindingContext as menuitem;
}
}
public class menuitem
{
public string title { get; set; }
}
As Jason said, we also suggest you can use ListView context menu to do, don't need to use SwipView. Select one item and long press to get context menu, like the following screenshot.
<ListView ItemsSource="{Binding items}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Clicked="OnMore" Text="More" />
<MenuItem Clicked="OnDelete" Text="Delete" />
</ViewCell.ContextActions>
<StackLayout>
<Label Text="{Binding title}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
private void OnMore(object sender, EventArgs e)
{
MenuItem item = sender as MenuItem;
menuitem model = item.BindingContext as menuitem;
}
About ListView interactivity, please take a look:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/interactivity
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…