msfanboy
msfanboy

Reputation: 5291

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 binding 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?

Upvotes: 1

Views: 979

Answers (3)

ArildF
ArildF

Reputation: 426

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.

Upvotes: 0

Jportelas
Jportelas

Reputation: 646

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.

Upvotes: 0

STO
STO

Reputation: 10658

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}" />

Upvotes: 1

Related Questions