Articles in this section
Category / Section

How to reset the swipe using MVVM in Xamarin.Forms ListView (SfListView)?

1 min read

You can reset the swiping after completing the SwipeView action using MVVM in Xamarin.Forms SfListView.

C#

Define the custom command parameter that has SfListView and the swiped ItemData.

internal class CustomCommandParameter
{
 #region Fields
 public SfListView ListView { get; set; }
 public object CommandParameter { get; set; }
 #endregion
 
 #region Constructor
 public CustomCommandParameter(object commandParameter, SfListView sfListView)
 {
  CommandParameter = commandParameter;
  ListView = sfListView;
 }
 #endregion
}

XAML

Define the EventToCommandBehavior for the Clicked event of the ImageButton and pass the SfListView as the CommandParameter.

<sync:SfListView x:Name="listView" ItemsSource="{Binding InboxInfo}" ItemTemplate="{StaticResource ItemTemplate}" HeaderTemplate="{StaticResource HeaderTemplate}" AllowSwiping="True" SelectionMode="None" IsStickyHeader="True" SwipeOffset="120" SwipeThreshold="30" ItemSize="100" HeaderSize="50" >
    <sync:SfListView.RightSwipeTemplate>
        <DataTemplate x:Name="RightSwipeTemplate">
            <Grid BackgroundColor="#DC595F" HorizontalOptions="Fill" VerticalOptions="Fill">
                <Grid VerticalOptions="Center" HorizontalOptions="Center">
                    <ImageButton x:Name="imageButton" HeightRequest="35" WidthRequest="35" BackgroundColor="Transparent" Source="Favorites.png">
                        <ImageButton.Behaviors>
                            <local:EventToCommandBehavior Command="{Binding Path=BindingContext.SetFavoriteCommand, Source={x:Reference listView}}" EventName="Clicked" CommandParameter="{x:Reference listView}"/>
                        </ImageButton.Behaviors>
                    </ImageButton>
                </Grid>
            </Grid>
        </DataTemplate>
    </sync:SfListView.RightSwipeTemplate>
</sync:SfListView>

C#

Create the custom command parameter with the swiped ItemData and SfListView in the OnEvent method of EventToCommandBehavior. Also, pass the custom command parameter to the event.

public class EventToCommandBehavior : BehaviorBase<ImageButton>
{
 void OnEvent(object sender, object eventArgs)
 {
  if (Command == null)
  {
   return;
  }
 
  object resolvedParameter = null;
  if (CommandParameter != null)
  {
   resolvedParameter = new CustomCommandParameter((sender as ImageButton).BindingContext, CommandParameter as SfListView);
  }
 
  if (Command.CanExecute(resolvedParameter))
  {
   Command.Execute(resolvedParameter);
  }
 }
}

C#

You can get the swiped ItemData and the ListView from the command parameter in the command handler. Reset the ListView swipe using the ResetSwipe method.

public class ListViewSwipingViewModel
{
    public Command<object> SetFavoriteCommand { get; set; }
 
    public ListViewSwipingViewModel()
    {
        SetFavoriteCommand = new Command<object>(SetFavorites);
    }
 
    private void SetFavorites(object obj)
    {
        var customCommandParameter = obj as CustomCommandParameter;
        (customCommandParameter.CommandParameter as ListViewInboxInfo).IsFavorite = !(customCommandParameter.CommandParameter as ListViewInboxInfo).IsFavorite;
        customCommandParameter.ListView.ResetSwipe();
    }
}

View sample in GitHub

demo image to reset swipe using MVVM in Xamarin.Forms SfListView

Conclusion

I hope you enjoyed learning about how to reset the swipe using MVVM in Xamarin.Forms ListView (SfListView).

You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms ListView documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied