2013-02-08 44 views
3

我有一个绑定到名称的ObservableCollection的列表框。列表中的一些项目将会有一个复选框被打开/关闭,表示该项目已被选中。为MVVM选择的多个ListBox /收集项目

如何在Master-Details概念之后的第一个列表框的选定项目中创建ObservableCollection?

(我打算用我的MasterViewModel作为在DataContext我的DetailsView控件显示所选项目集合。)

提前感谢!

回答

5

是的,我之前也遇到过这个。 ListBox和类似的有一个名为'SelectedItem'的依赖项属性,但'SelectedItems'(带有's')属性不是作为一个实现的。

我找到的最干净的解决方案只是为了列表框子类,并创建我自己的依赖属性,名为'SelectedItems'。没有乐趣,但它是我认为最好的解决方案。

UPDATE

首先我们的视图模型:

class ViewModel : INotifyPropertyChanged 
{ 
    // Set up our collection to be read from the View 
    public ObservableCollection<String> Collection { get; private set; } 

    // This collection will maintain the selected items 
    public ObservableCollection<String> SelectedItems { get; private set; } 

    public ViewModel() 
    { 
     // Instantiate 
     this.Collection = new ObservableCollection<String>(); 
     this.SelectedItems = new ObservableCollection<String>(); 

     // Now let's monitor when this.SelectdItems changes 
     this.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged; 

     // Fill our collection with some strings (1 to 10). 
     // (1) Generate the numbers 1 - 10 
     // (2) Convert each number to a string 
     // (3) Cast into a list so we can use foreach 
     // (4) Add each item to the collection. 
     Enumerable.Range(1, 10) 
      .Select(number => number.ToString()) 
      .ToList()        
      .ForEach(this.Collection.Add); 

     // Remember! Never reset the ObservableCollection. 
     // That is, never say this.Collection = new... (or you'll break the binding). 
     // instead use this.Collection.Clear(), and then add the items you want to add 
    } 

    void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
     if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) 
     { 
      foreach (String str in this.SelectedItems) 
       System.Diagnostics.Debug.WriteLine("New item added {0}", str); 

     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

然后我们的扩展ListBoxEx:

class ListBoxEx : ListBox 
{ 
    // Use the 'new' keyword so that we 'hide' the base property. 
    // This means that binding will go to this version of SelectedItems 
    // rather than whatever the base class uses. To reach the base 'SelectedItems' property 
    // We just need to use base.SelectedItems instead of this.SelectedItems 
    // Note that we register as an observable collection. 
    new DependencyProperty SelectedItemsProperty = 
     DependencyProperty.Register("SelectedItems", typeof(ObservableCollection<String>), typeof(ListBoxEx)); 

    // Accessor. Again, note the 'new'. 
    new public ObservableCollection<String> SelectedItems 
    { 
     get { return (ObservableCollection<String>) GetValue(SelectedItemsProperty); } 
     set { SetValue(SelectedItemsProperty, value); } 
    } 

    protected override void OnSelectionChanged(SelectionChangedEventArgs e) 
    { 
     // Guard against ViewModel being null 
     if (this.SelectedItems != null) 
     { 
      // Clear the list 
      this.SelectedItems.Clear(); 

      // (1) On selection changed. Get the new base.SelectedItems 
      // (2) Cast each item to a String ("Make a string collection") 
      // (3) Cast to list, and use foreach to add each item to 
      // this.SelectedItems (note this is different from the original base.SelectedItems) 
      base.SelectedItems.Cast<String>() 
       .ToList() 
       .ForEach(this.SelectedItems.Add); 
     } 
    } 
} 

最后我们的观点:

<Window.DataContext> 
    <lol:ViewModel /> 
</Window.DataContext> 
<Grid> 
    <lol:ListBoxEx ItemsSource="{Binding Collection}" SelectedItems="{Binding SelectedItems}" 
        SelectionMode="Multiple"/> 
</Grid> 
+0

我在思考更多沿着向视图模型添加“isSelected”bool的方法,然后在新的“details”viewmodel创建一个ObservableCollection,这是真的。我不确定这将如何解决。 至于你的“在列表框中的多个选择”评论 - 我有一种感觉,这可以通过设置SelectionMode =“Multiple”或SelectionMode =“Extended”来解决。 – Rachael

+0

好吧,将isSelected布尔添加到对象本身将工作。但是'isSelected'是一个视图属性,而不是一个model/viewModel属性 - 所以当它工作时,它在技术上是不正确的。另外,在您的原始文章中,听起来好像您要填充由所选项目组成的集合。问题是要把这些项目放到vm中,你必须有一个依赖属性,但是不存在这样的事情 - 你不得不自己做一个。 – sircodesalot

+0

哦不是对象,而是_viewmodel_对象。有人在另一个问题中指出,乔希史密斯在他的博客中这样做。 – Rachael