开发者

WPF: Binding DataGrid to a list<Product> having a DataGridComboBoxColumn bound to a list<Category>?

I have a DataGrid with ItemsSource set to a list of products and I have a DataGridComboBoxColumn inside the DataGrid with ItemsSource set to a list of categories. That way I want the user to choose a certain category for each product.

I always get the bindin开发者_开发知识库g error:

BindingExpression path error: 'Categories' property not found on 'object' ''Product' (Hash)

Well I do not want to make the Category list part of the Product entity as 1:N relation, although it would work that way.

I want to keep them separate.

Anyone knows a workaround?


Create class with static property like

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

and use following binding

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


this is kind of late reply but in order to share the knowledge I found this:

Binding a WPF DataGridComboBoxColumn with MVVM

This answer shows that is not always mandatory to convert the second list to a static class, you can always specify a RelativeSource and search for an specific Ancestor and then bind to the "other" list you have in your ViewModel.


This is probably relevant to your problem.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜