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 ); } } }
source share