2010-08-20 72 views
1

我有一個包含很多項目的列表框,並且我在C#WPF中有一個組合框。將列表框中的某些項綁定到combox項目源

我想將combobox的itemsource綁定到listbox的itemsource,但我想過濾掉一些項目(這是一個xml數據源,我想過濾項目元素的某個元素的內容)。 實施例項:

<Item> 
<itemtype>A</itemtype> 
<itemname>Name</itemname> 
</item> 

    <Item> 
<itemtype>B</itemtype> 
<itemname>Name</itemname> 
</item> 

我想的添加項目時到列表框但隨後的項目的「姓名」值時,它在列表框被改變,不更新手動將項目添加到組合框。

這樣做的好方法是什麼?假設我只想顯示帶有itemtype B的所有項目名稱。是否可以在wpf綁定中完成,還是需要在後面執行一些代碼?

回答

2

它是對數據源的引用,例如,如果您使用MVC模式,則將其添加到視圖模型中的集合中。令人驚奇的是,它的使用非常簡單。該視圖被更新並具有它自己的刷新處理。我將做一個小例子:

在WPF:

<ListBox ItemsSource={Binding Path=MySource} ItemTemplate="{StaticResource myItemTemplate}" /> 

的代碼邏輯:

public class Item 
{ 
    public string Name { get; set; } 
    public string Type { get; set; } 
} 

public class MyViewModel 
{ 
    public ObservableCollection<Item> MySource { get; set; } 

    public MyViewModel() 
    { 
     this.MySource = new ObservableCollection<Item>(); 
     this.MySource.Add(new Item() { Name = "Item4", Type = "C" }); 
     this.MySource.Add(new Item() { Name = "Item1", Type = "A" }); 
     this.MySource.Add(new Item() { Name = "Item2", Type = "B" }); 
     this.MySource.Add(new Item() { Name = "Item3", Type = "A" }); 

     // get the viewsource 

     ListCollectionView view = (ListCollectionView)CollectionViewSource 
      .GetDefaultView(this.MySource); 

     // first of all sort by type ascending, and then by name descending 
     view.SortDescriptions.Add(new SortDescription("Type", ListSortDirection.Ascending)); 
     view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Descending)); 

     // now i like to group the items by type 
     view.GroupDescriptions.Add(new PropertyGroupDescription("Type")); 

     // and finally i want to filter all items, which are of type C 
     // this is done with a Predicate<object>. True means, the item will 
     // be shown, false means not 
     view.Filter = (item) => 
      { 
       Item i = item as Item; 
       if (i.Type != "C") 
        return true; 
       else 
        return false; 
      }; 


     // if you need a refreshment of the view, because of some items were not updated 
     view.Refresh(); 

     // if you want to edit a single item or more items and dont want to refresh, 
     // until all your edits are done you can use the Edit pattern of the view 

     Item itemToEdit = this.MySource.First(); 
     view.EditItem(itemToEdit); 
     itemToEdit.Name = "Wonderfull item"; 

     view.CommitEdit(); 

     // of course Refresh/Edit only makes sense in methods/callbacks/setters not 
     // in this constructor 
    } 

} 

有趣的是,這種模式將直接影響到GUI列表框中。如果添加分組/排序,這將影響列表框的顯示行爲,即使itemssource只綁定到視圖模型。

+0

感謝您的詳細解釋。那麼如何將我當前的數據源設置爲observablecollection? 所以根據你的解釋我過濾該集合。我是否將集合綁定到代碼或xaml中的組合框? – internetmw 2010-08-21 22:56:11

+0

這是非常有益的,謝謝! – 2011-05-05 22:01:00

2

在這種情況下綁定的問題是,它只能設置一次。所以如果你綁定它,源是同步的。您可以使用轉換器進行綁定,以過濾不想綁定的項目。另一種方法是使用WPF的精彩CollectionViewSource。

您可以在任何種類的ItemsSource上將分組/排序和過濾器添加到CollectionViewSource。例如,使用ListCollectionView。

ListCollectionView view = 
    (ListCollectionView)CollectionViewSource.GetDefaultView(yourEnumerableSource); 
+0

所以'yourEnumerableSource'是對列表框還是對數據源的引用?當數據源發生變化時,更新的視圖是? – internetmw 2010-08-20 23:30:00

+0

看到第二個帖子 – JanW 2010-08-21 08:54:26

相關問題