I'm using a WinUI3 ListView like this to load a list of files:
<ListView ItemTemplate="{StaticResource Template2}"
ItemsSource="{x:Bind Files,Mode=OneWay}"
IsItemClickEnabled="True" x:Name="List2" SelectionMode="Multiple" />
<DataTemplate x:Key="Template2" x:DataType="local:FileItem">
<Grid>
.... TextBlocks that bind to properties in FileItem
</Grid>
</DataTemplate>
Now, how do I configure my DataTemplate in order for some (or all) of the items to be selected by default?
Is there a special value in the data template I should use?
You can use the
ListViewItemin yourItemTemplate.Let's say your
FileItemlooks like this:and your ViewModel:
and in XAML:
But unfortunately, this won't work because the
ListViewItemresets itsIsSelectedproperty tofalsewhen it's loaded. I'm not sure if it's a bug or it's by design.As a workaround, you can do the binding after each
ListViewItemis loaded:then in code-behind:
BTW, I'm using the CommunityToolkit.Mvvm NuGet package for the ViewModels but I hope this gives you an idea how to fix your issue.