Link data to parent text datacontext in XAML

The user control that includes these 2 elements has the ColumnTypes property.

Each of these elements refers with respect to the same expression to the main data file, but the former does not work, but the latter does.

Do you have any ideas how to research this?

<DataGrid x:Name="DataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Table}" > <DataGrid.Columns> <DataGridComboBoxColumn Header="Type" > <DataGridComboBoxColumn.ItemsSource> <Binding Path="DataContext.GetColumnTypes" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" /> </DataGridComboBoxColumn.ItemsSource> </DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid> <ComboBox Grid.Row="1"> <ComboBox.ItemsSource> <Binding Path="DataContext.GetColumnTypes" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" /> </ComboBox.ItemsSource> </ComboBox> 

Error System.Windows.Data: 4: Cannot find source for binding with reference "RelativeSource FindAncestor, AncestorType =" System.Windows.Controls.UserControl ", AncestorLevel = '1' '. BindingExpression: Path = DataContext.GetColumnTypes; DataItem = NULL; the target is "DataGridComboBoxColumn" (HashCode = 53813616); target is "ItemsSource" (type "IEnumerable")

+4
source share
2 answers

This is a known limitation of DataGridComboBoxColumn .

You can see on MSDN what things you can bind to the ItemsSource property. A regular property is not one of them, so your case will not work.

Another way to achieve what you want is to create a DataGridTemplateColumn that contains a ComboBox .

In your case, it will look something like this:

 <DataGrid.Columns> <DataGridTemplateColumn Header="Type"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding DataContext.GetColumnTypes, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> 
+8
source

DataGridColumn does not lie in the Visual Tree DataGrid, so it can't inherit its DataContext . But there are workarounds for it, that is, you can explicitly provide a DataContext to your DataGridColumns, whose data can be found Provide DataContext columns for DataGrid columns .

Also, I personally like the approach described here - Inheriting a parent DataContext using Freezable inheritance using the Freezable class.

The code from the first link in case the link does not work in the future -

Add this to your App.xaml.cs in App() constructor -

 FrameworkElement.DataContextProperty.AddOwner(typeof(DataGridColumn)); FrameworkElement.DataContextProperty.OverrideMetadata ( typeof(DataGrid), new FrameworkPropertyMetadata (null, FrameworkPropertyMetadataOptions.Inherits, new PropertyChangedCallback(OnDataContextChanged))); 

The OnDataContextChanged callback simply redirects the DataContext from the DataGrid to its columns:

 public static void OnDataContextChanged ( DependencyObject d, DependencyPropertyChangedEventArgs e) { DataGrid grid = d as DataGrid ; if ( grid != null ) { foreach ( DataGridColumn col in grid.Columns ) { col.SetValue ( FrameworkElement.DataContextProperty, e.NewValue ); } } } 
+3
source

All Articles