如何在 Windows 8.1 中使用 Behaviors SDK 绑定具有多个 SelectionMode 的 ListView 以实现 MVVM [英] How to bind a ListView with SelectionMode multiple to achieve MVVM using Behaviors SDK in Windows 8.1

查看:23
本文介绍了如何在 Windows 8.1 中使用 Behaviors SDK 绑定具有多个 SelectionMode 的 ListView 以实现 MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个选择模式的 ListView 的 Windows 8.1 应用程序(使 ListView 允许用户选择多个条目)

I have a Windows 8.1 application with a ListView with SelectionMode multiple(making the ListView allow the user to select multiple entries)

我正在尝试使用 Microsoft 的 Behaviors SDK

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>

如您所见,ListView 的 ItemSource 已绑定到属性 Data 上,该属性是我的 ViewModel 中的列表,如下所示

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; }
}

我能想到的方法是,如果在我的视图中选择了相应的元素,则在类 MyData 中将 IsSelected 属性设置为 true.但该解决方案看起来我需要隐藏代码.

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.

我已经为我的 ListView 附加了 EventTriggerBehavior,但我无法弄清楚如何绑定它以及将它绑定到什么.

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.

如果有人能指出我正确的方向来实现这一点,我会很高兴,这样我的列表数据就会有数据让我区分我的 SelectedItems 和非选中项.或者更好的是,如果我可以完全消除 IsSelected bool 属性,那就太好了.

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.

提前致谢.

推荐答案

查看 BindableSelectionWinRT XAML 工具包.

您可以在 XAML 中按以下方式使用它:

You can use it the following way in your XAML:

xmlns:Extensions="using:WinRTXamlToolkit.Controls.Extensions"
Extensions:ListViewExtensions.BindableSelection="{Binding SelectedItems, Mode=TwoWay}"

这篇关于如何在 Windows 8.1 中使用 Behaviors SDK 绑定具有多个 SelectionMode 的 ListView 以实现 MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆