How to bind a ListView with SelectionMode multiple to achieve MVVM using Behaviors SDK in Windows 8.1

这一生的挚爱 提交于 2019-12-25 06:21:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!