I’m currently trying to do some binding inside of a datagrid but I’m having problems getting up to the level of DataContext of the view.
Here is the code:
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Operators}"
ItemsSource="{Binding DataContext.OperatorList,ElementName=FilterGrid}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
Any ideas on whats wrong? The View’s Viewmodel is connected in the code behind.
EDIT: The Binding that is not working is the ItemsSource binding shown above
When you use the
DataTemplateof theDataGrid, you cannot useElementNamebindings as it won’t resolve properly due to limitations in the resolution capabilities ofFindControlwithin the DataGrid control hierarchy. You need to use aRelativeSourcebinding that travels up the control tree looking for a specific control type (which you need to determine – from your element name I assumed it was aDataGridancestor type).See this SO post that shares some potentially related sample code using MVVM to access the
DataContextof theUserControlhost to populate a ComboBoxItemsSource.