2010-06-18 79 views
1

我有一個DataSource的ItemsSource設置爲產品列表和 我有DataGrid內的DataGridComboBoxColumn與ItemsSource設置爲類別列表。這樣我希望用戶爲每個產品選擇一個特定的類別。WPF:將DataGrid綁定到列表<Product>將DataGridComboBoxColumn綁定到列表<Category>?

我總是得到綁定錯誤:

BindingExpression路徑錯誤:「分類」屬性不是「對象」「」產品」(井)

好吧,我不希望讓類別列表中找到作爲1:N關係的產品實體的一部分,儘管它會以這種方式工作。

我想讓他們分開。

任何人都知道解決方法?

回答

1

創建靜態屬性類像

static class ValueLists 
{ 
    public static IEnumerable<Category> Categories {get {... }} 
} 

和使用後綁定

ItemsSource="{x:Static myNs:ValueList.Categories}" /> 
+0

我發現一個很好的鏈接處理完全相同的東西,我需要: http://stackoverflow.com/questions/1633800/wpf-datagrid-datagridcomboxbox-itemssource-binding-to-a-collection-of-collection – msfanboy 2010-06-19 14:13:29

+0

我不能在靜態類實現INotifyPropertChanged我需要;-) 而不是靜態綁定wouldnt工作? => ItemsSource =「{Binding Path = DataContext.Categories, RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type Window}}}」 IsReadOnly =「True」Background =「White」/> – msfanboy 2010-06-19 20:05:50

0

This可能是有關您的問題。

What is happening here?
The Columns collection is just a property in the Datagrid; this collection is not in the logical (or visual) tree, therefore the DataContext is not being inherited, which leads to there being nothing to bind to.

0

這是有點晚了答覆,但爲了分享我發現這個知識:

Binding a WPF DataGridComboBoxColumn with MVVM

這個回答表明,並不總是強制性的第二個列表轉換爲靜態類,你總是可以指定一個RelativeSource並搜索特定的Ancestor,然後綁定到ViewModel中的「其他」列表。