问题
I have a Windows 8.1 application with a ListView with SelectionMode multiple(making the ListView allow the user to select multiple entries)
I am trying to use Behaviors SDK from Microsoft
<DataTemplate x:Key="DataItemTemplate">
<TextBlock Text="{Binding Name}" Margin="10,0,0,0"/>
</DataTemplate>
<ListView ItemsSource="{Binding Data}"
SelectionMode="Multiple"
ItemTemplate="{StaticResource DataItemTemplate}">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior SourceObject="{Binding}" EventName="SelectionChanged"/>
</Interactivity:Interaction.Behaviors>
</ListView>
As you can see the ItemSource of the ListView has been bound to the property Data which is a List in my ViewModel as shown below
public List<MyData> Data { get; set; }
class MyData
{
public string Name { get; set; }
public bool IsSelected { get; set; }
}
The approach that I could think of was to set the IsSelected property to true in the class MyData if the corresponding element has been selected in my View. But that solution looks like I need to have codebehind.
I have attached the EventTriggerBehavior for my ListView but I am unable to figure out how to Bind it and what to bind it to.
I would be very glad if someone can point me in the right direction to achieve this so that my List Data will have the data for me to differentiate between my SelectedItems and non-selected items. Or better still if I can completely eliminate the IsSelected bool property it would be great.
Thanks in Advance.
回答1:
Check out BindableSelection in WinRT XAML Toolkit.
You can use it the following way in your XAML:
xmlns:Extensions="using:WinRTXamlToolkit.Controls.Extensions"
Extensions:ListViewExtensions.BindableSelection="{Binding SelectedItems, Mode=TwoWay}"
来源:https://stackoverflow.com/questions/25429590/how-to-bind-a-listview-with-selectionmode-multiple-to-achieve-mvvm-using-behavio