大佬教程收集整理的这篇文章主要介绍了silverlight-3.0 – 使用ViewModel中的集合在Muliselect列表框中同步SelectedItem,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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.@R_801_6938@ue(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.@R_801_6938@ue(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所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。