views:

49

answers:

2

Hi there,

I am new to the MVVM model, so I would like some advice, the solution I'm using right now does not seem to work.

Description: I have a Viewmodel containing an observablecollection of Customers, I display these in a grid with "Back" and "Forward" buttons. The grids datacontext is set to the SelectedCustomer which is a property in my viewmodel.

So far so good...

However, the customers have a statusfield, which is the integer of the statusrow in the database. In my viewmodel I have this list (observablecollection) populated, but when I try to populate a combobox in the CustomerGrid there is no data displayed. I guess this is because the Datacontext on the grid is the SelectedCustomer property, and the statuslist is in the root viewmodel.

The Question is this, how can my combobox access my statuslist from within the customergrid, or how should I tackle this situation?

Best regards, Peter Larsson!

A: 
{Binding DataContext.ListOfStatus, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TypeOfParentControl}}}

You can bind to parent's data context

Hasan Khan
Ok, sounds logical enough, my grid is in a StackPanel, so using the following should work: <ComboBox ItemsSource="{Binding DataContext.StatusGroups, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" DisplayMemberPath="StatusName" Grid.Column="1" Grid.Row="6"/>But it doesn't, the combobox is still empty.Any ideas?
Peter Larsson
The problem was that I didn't set reference to my viewmodel in the XAML but in my App.xaml. When I declared my viewmodel as a staticresource it worked.
Peter Larsson
A: 

Hi,

some grids allow binding of combobox columns too. That depends on the grid - I never used a grid in WPF or Silverlight so take it just as a hint.

Kind regards

Sascha