silverlight-3.0 – 使用ViewModel中的集合在Muliselect列表框中同步SelectedItem

silverlight-3.0 – 使用ViewModel中的集合在Muliselect列表框中同步SelectedItem,第1张

概述我使用棱镜在SL3应用程序中有一个多选列表框,我需要在我的viewmodel中包含列表框中当前选定的项目的集合。 viewmodel不了解视图,因此无法访问列表框控件。另外,我需要能够从viewmodel中清除列表框中的所选项目。 不知道如何解决这个问题 谢谢 迈克尔 因此,假设您有一个具有以下属性的ViewModel: public ObservableCollection<string> Al 我使用棱镜在SL3应用程序中有一个多选列表框,我需要在我的viewmodel中包含列表框中当前选定的项目的集合。

viewmodel不了解视图,因此无法访问列表框控件。另外,我需要能够从viewmodel中清除列表框中的所选项目。

不知道如何解决这个问题

谢谢
迈克尔

解决方法 因此,假设您有一个具有以下属性的viewmodel:

public ObservableCollection<string> Allitems { get; private set; }public ObservableCollection<string> SelectedItems { get; private set; }

您将首先将Allitems集合绑定到ListBox:

<ListBox x:name="MyListBox" ItemsSource="{Binding Allitems}" SelectionMode="Multiple" />

问题是ListBox上的SelectedItems属性不是DependencyProperty。这是非常糟糕的,因为您无法将其绑定到viewmodel中的某个东西。

第一种方法是将这个逻辑放在代码隐藏中,调整viewmodel:

public MainPage(){    InitializeComponent();    MyListBox.SelectionChanged += ListBoxSelectionChanged;}private static voID ListBoxSelectionChanged(object sender,SelectionChangedEventArgs e){    var ListBox = sender as ListBox;    if(ListBox == null) return;    var viewmodel = ListBox.DataContext as MainVM;    if(viewmodel == null) return;    viewmodel.SelectedItems.Clear();    foreach (string item in ListBox.SelectedItems)    {        viewmodel.SelectedItems.Add(item);    }}

这种做法会奏效,但真的很丑陋。我的首选方法是将此行为提取为“附加行为”。如果您这样做,您可以完全消除您的代码隐藏并将其设置在XAML中。奖金是这个“附加行为”现在可以在任何ListBox中重用:

<ListBox ItemsSource="{Binding Allitems}" Demo:SelectedItems.Items="{Binding SelectedItems}" SelectionMode="Multiple" />

这里是附加行为的代码:

public static class SelectedItems{    private static Readonly DependencyProperty SelectedItemsBehaviorProperty =        DependencyProperty.Registerattached(            "SelectedItemsBehavior",typeof(SelectedItemsBehavior),typeof(ListBox),null);    public static Readonly DependencyProperty ItemsProperty = DependencyProperty.Registerattached(            "Items",typeof(IList),typeof(SelectedItems),new PropertyMetadata(null,ItemsPropertyChanged));    public static voID SetItems(ListBox ListBox,IList List) { ListBox.SetValue(ItemsProperty,List); }    public static IList GetItems(ListBox ListBox) { return ListBox.GetValue(ItemsProperty) as IList; }    private static voID ItemsPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)    {        var target = d as ListBox;        if (target != null)        {            GetorCreateBehavior(target,e.NewValue as IList);        }    }    private static SelectedItemsBehavior GetorCreateBehavior(ListBox target,IList List)    {        var behavior = target.GetValue(SelectedItemsBehaviorProperty) as SelectedItemsBehavior;        if (behavior == null)        {            behavior = new SelectedItemsBehavior(target,List);            target.SetValue(SelectedItemsBehaviorProperty,behavior);        }        return behavior;    }}public class SelectedItemsBehavior{    private Readonly ListBox _ListBox;    private Readonly IList _boundList;    public SelectedItemsBehavior(ListBox ListBox,IList boundList)    {        _boundList = boundList;        _ListBox = ListBox;        _ListBox.SelectionChanged += OnSelectionChanged;    }    private voID OnSelectionChanged(object sender,SelectionChangedEventArgs e)    {        _boundList.Clear();        foreach (var item in _ListBox.SelectedItems)        {            _boundList.Add(item);        }    }}
总结

以上是内存溢出为你收集整理的silverlight-3.0 – 使用ViewModel中的集合在Muliselect列表框中同步SelectedItem全部内容,希望文章能够帮你解决silverlight-3.0 – 使用ViewModel中的集合在Muliselect列表框中同步SelectedItem所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1008260.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存