<datagrid> ... extensions:DataGridExtension.DataContextForColumns="{Binding Mode=OneWay}" >
public class DataGridExtension { static DataGridExtension() { try { //Allows to set DataContextProperty on the columns. Must only be invoked once per application. FrameworkElement.DataContextProperty.AddOwner(typeof (DataGridColumn)); } catch { } } public static object GetDataContextForColumns(DependencyObject obj) { return obj.GetValue(DataContextForColumnsProperty); } public static void SetDataContextForColumns(DependencyObject obj, object value) { obj.SetValue(DataContextForColumnsProperty, value); } /// <summary> /// Allows to set DataContext property on columns of the DataGrid (DataGridColumn) /// </summary> /// <example><datagridtextcolumn header="{Binding DataContext.ColumnHeader, RelativeSource= {RelativeSource Self}}" /></example> public static readonly DependencyProperty DataContextForColumnsProperty = DependencyProperty.RegisterAttached( "DataContextForColumns", typeof(object), typeof(DataGridExtension), new UIPropertyMetadata(OnDataContextChanged)); /// <summary> /// Propogates the context change to all the DataGrid's columns /// </summary> private static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var grid = d as DataGrid; if (grid == null) return; foreach (DataGridColumn col in grid.Columns) col.SetValue(FrameworkElement.DataContextProperty, e.NewValue); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)