I have a collection of view model objects bound to a ListView. I want to add a selection indicator for the selected ListViewItem, like in the dark blue rectangle below:
What is the right way to implement this?
What I plan to do: Add an IsSelected field to my view model, set it to true when item is selected and false otherwise, then bind it to visibility of the NotificationSelectedIndicator rectangle.
Please take a look at code below for reference:
<DataTemplate x:Key="NotificationListViewItemTemplate" x:Name="NotificationsListViewDataTemplate" x:DataType="local:NotificationViewModel">
<StackPanel Orientation="Horizontal">
<Rectangle Name="NotificationSelectedIndicator" Fill="{ThemeResource SystemAccentColor}" HorizontalAlignment="Left" Width="5" Height="Auto" Margin="-11, 0, 0, 0" Visibility="{x:Bind Converter={StaticResource BoolToVisConverter}, Path=IsSelected}"/>
<Grid Height="Auto" Width="Auto" Padding="0, 10, 0, 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
...
</Grid>
</StackPanel>
</DataTemplate>
Is there a simpler way to add the indicator?
In the documentation page for ListViewItem, there's a resource called ListViewItemSelectionIndicatorVisualEnabled, but these are only supported for WinUI.
Thank you for any help in advance!