Slidable List View For Windows 10 UWP App Using Community ToolKit

Windows 10 UWP Community toolkit is a collection of a helper class, custom controls and Application services. The SlideableListItem Control enables the actions to be triggered by sliding the content left or right.

Create a new Windows 10 UWP project or open your existing project. Right click on your project and select Manage NuGet Packages. Search for Microsoft.Toolkit.UWP.UI.Controls and install it, as shown below-

Microsoft.Toolkit.UWP.UI.Controls

This will add Microsoft.Toolkit.Uwp.UI.Controls.dll in 'References' of your project, as shown below-

Microsoft.Toolkit.Uwp.UI.Controls.dll

Add the toolkit reference in your XAML pages, using the code, given below-

XAML

xmlns:UWPToolkit="using:Microsoft.Toolkit.Uwp.UI.Controls"

Now, open your XAML page and write the code, given below-

  1. Page.Resources>  
  2.   
  3. <DataTemplate x:Key="EmailsItemTemplate" >  
  4. <UWPToolkit:SlidableListItem LeftIcon="Add"  
  5. RightIcon="Delete"  
  6.   
  7. HorizontalAlignment="Stretch"  
  8.   
  9. RightCommandRequested="SlidableListItem_RightCommandActivated"  
  10.   
  11. MinWidth="300"  
  12. MaxWidth="800"  
  13. >  
  14. <Grid Background="AliceBlue">  
  15. <Grid.ColumnDefinitions>  
  16. <ColumnDefinition Width="50"/>  
  17. <ColumnDefinition Width="*" />  
  18. </Grid.ColumnDefinitions>  
  19. <Grid Grid.Column="0" Background="SkyBlue" x:Name="Checkmark" >  
  20. <SymbolIcon Symbol="Read" Foreground="White" />  
  21. </Grid>  
  22. <TextBlock Grid.Column="1" Text="{Binding Title}" ></TextBlock>  
  23. </Grid>  
  24. </UWPToolkit:SlidableListItem>  
  25. </DataTemplate>  
  26. </Page.Resources>  
  27. <Grid x:Name="Root" Padding="24">  
  28. <ListView x:Name="listView" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Center" ItemsSource="{x:Bind _items, Mode=OneWay}" ItemTemplate="{StaticResource EmailsItemTemplate}">  
  29. <ListView.ItemContainerStyle>  
  30. <Style TargetType="ListViewItem">  
  31. <Setter Property="HorizontalContentAlignment" Value="Stretch"/>  
  32. <Setter Property="Margin" Value="0,1"/>  
  33. </Style>  
  34. </ListView.ItemContainerStyle>  
  35. </ListView>  
  36. </Grid>  
We need to create Data Template to list the contents and place the slidableListItem control inside the data template. Using RightCommandRequest and LeftCommandRequest, we can perform the action, while user slides left or right.

Now, go to the code at the backend page and write the code, given below, to list the item.
  1. private ObservableCollection < Item > _items;  
  2. public MainPage()  
  3. {  
  4.     this.InitializeComponent();  
  5.     ObservableCollection < Item > items = new ObservableCollection < Item > ();  
  6.     items.Add(new Item() {  
  7.         Title = "Suresh"  
  8.     });  
  9.     items.Add(new Item() {  
  10.         Title = "Microsoft MVP"  
  11.     });  
  12.     items.Add(new Item() {  
  13.         Title = "C# Corner MVP"  
  14.     });  
  15.     items.Add(new Item() {  
  16.         Title = "Windows 10 UWP"  
  17.     });  
  18.     _items = items;  
  19. }  
  20. public class Item {  
  21.     public string Title {  
  22.         get;  
  23.         set;  
  24.     }  
  25. }  
  26. private void SlidableListItem_RightCommandActivated(object sender, EventArgs e) {}  
Now, run the app and see how the output looks, as shown below-

app