We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

[SfListView] SelectedItem on swipe

Hi guys,

I spend a whole evening figuring out how to get de selected item in the commandparameter inside the  DataTemplate of the StartSwipeTemplate.
Somehow there is no way to get the actual item as SelectedItem. The only way is to handle the event and set the selecteditem of the SfListview manually. Is this behavior deliberate or did I miss something?

Setting the the commandparameter to "{Binding}" did also not work. It refers to the ViewModel of the page.

listview:SfListView.StartSwipeTemplate>
                <DataTemplate x:Name="SwipeLeft">
                    <ImageButton
                        ToolTipProperties.Text="{m:GetString Edit}"
                        Command="{Binding Source={RelativeSource AncestorType={x:Type listview:SfListView}}, Path=BindingContext.Edit_Command}"
                        CommandParameter="{Binding Source={RelativeSource AncestorType={x:Type listview:SfListView}}, Path=SelectedItem}"
                        BackgroundColor="Green"
                        Source="edit.png"
                        WidthRequest="75"
                        HeightRequest="75"
                        Scale="0.75"
                        VerticalOptions="Center"/>
                </DataTemplate>
            </listview:SfListView.StartSwipeTemplate>


This is how I resolved it now:

private void SfListView_SwipeStarting(object sender, Syncfusion.Maui.ListView.SwipeStartingEventArgs e)
    {
        if (BindingContext is BudgetsViewModel viewModel && e.DataItem is Budget budget)
            viewModel.SelectedItem = budget;
    }

Frankly, I don't like these kind of hacks.





 


1 Reply

SY Suthi Yuvaraj Syncfusion Team January 6, 2023 01:56 PM UTC

Hi  Ismail ,


We have checked the reported query “SelectedItem on swipe”,By default, In SfListView, item will not be selected during the swiping operation. To add item selection during swiping action, you can manually set the selected item through SwipingStarting event as you updated. If perform swiping after selected that same item, the selection will be maintained. This is current working behavior of Swiping feature. So in this case, you can get the selected item in the edit command, by passing the command parameter as selected item as like in your code snippet.


UG Link:

Swiping: https://help.syncfusion.com/maui/listview/swiping

Selection: https://help.syncfusion.com/maui/listview/selection


Regards,

Suthi Yuvaraj.


Loader.
Up arrow icon