views:

326

answers:

1

Admittedly I am new to WPF but I have looked and looked and can't find a solution to this problem.

I have a simple object like:

class Item
{
  ....

  public String Measure { get; set; }
  public String[] Measures {get; }
}

Which I am trying to bind to a DataGrid with two text columns and a combo box column. For the combo box column, propery Measure is the current selection and Measures the possible values.

My XAML is:

                        <DataGridComboBoxColumn Header="Measure" Width="Auto"
                                                SelectedItemBinding="{Binding Path=Measure}"
                                                ItemsSource="{Binding Path=Measures}"/>

                    </DataGrid.Columns>
                </DataGrid>

The text column are displayed just fine but the combobox is not - the values are not displayed at all. The binding error is :

¨System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Measures; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=11497055); target property is 'ItemsSource' (type 'IEnumerable')

How do I fix this???

Thanks

+1  A: 

The problem lies in that Columns does no inherit DataContext.

See more here http://stackoverflow.com/questions/502389/binding-in-a-wpf-data-grid-text-column

here blogs.msdn.com/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx

and here http://blogs.msdn.com/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx

Tendlon
Well, if that is true then why do the text columns work but the combo box column doesn't??
everwicked
If I understand it correctly it is because the text columns are bound directly to a property in the DataGrid's ItemsSource,DataGrid.ItemsSource->Item->Measure and the ComboBox are implicitly bound to DataContext of the Item, which is not inherited. DataGrid.ItemsSource->Item->DataContext->Measures/MeasureThis is how I understood it, admittedly I see now that I am not very good at explaining it. Possibly someone can do it better than me.
Tendlon