DataGrid Columns dont come under the visualtree of their parent
. Thats why they cannot inherit the DataContext
from parent nor they can refer to Ancestor.
DataGrid rows and cells on the other hand comes under the visualtree and hence can find ancestor and inherit DataContext.
In order to bind the Column, you will need to use the BindingProxy
.
To do it what you can do is define one resource in your Window Resource as
public class BindingProxy : Freezable
{
#region Overrides of Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
<DataGrid.Resources>
<local:BindingProxy x:Key="ProxyElement" Data="{Binding}" />
</DataGrid.Resources>
and then use this element to bind in column like
<DataGridComboBoxColumn Header="Enum1" Width="*"
ItemsSource="{Binding Path=Data.MyOptions, Source={StaticResource ProxyElement}"
SelectedItemBinding="{Binding Enum1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource MyConverter}}">
</DataGridComboBoxColumn>