wpf 数据绑定到 XAML 中的父数据上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15681156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Databinding to a Parent datacontext in XAML
提问by nicolas
The user control into which those 2 elements live has a property called ColumnTypes.
这两个元素所在的用户控件有一个名为 ColumnTypes 的属性。
Each of those elements refer relatively with the same expression to the main datacontext, yet the first one does notwork, while the latter does.
每一元素用相同的表达到主的datacontext相对参考,但第一个广告不工作,而后者则。
Do you have any idea how to investigate that ?
你知道如何调查吗?
<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>
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext.GetColumnTypes; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=53813616); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data 错误:4:无法找到引用“RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''的绑定源。BindingExpression:Path=DataContext.GetColumnTypes; 数据项=空;目标元素是“DataGridComboBoxColumn”(HashCode=53813616);目标属性是“ItemsSource”(类型“IEnumerable”)
回答by Peter Hansen
This is a known limitation of the DataGridComboBoxColumn.
这是DataGridComboBoxColumn.
You can see on MSDNwhat kind of things you can bind to its ItemsSourceproperty. A regular property is not one of them, so your case wont work.
你可以在 MSDN 上看到你可以绑定到它的ItemsSource属性的东西。常规财产不是其中之一,因此您的情况将不起作用。
A different way to achieve what you want, is to make a DataGridTemplateColumnwhich contains a ComboBox.
实现您想要的另一种方法是制作DataGridTemplateColumn包含ComboBox.
In your case that would 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>
回答by Rohit Vats
DataGridColumnnot lies in the Visual Treeof DataGrid hence it can't inherit its DataContext. But there are workarounds for it i.e. you can explicitly supply DataContext to your DataGridColumns whose details can be found Provide DataContext to DataGrid Columns.
DataGridColumn不在于Visual TreeDataGrid 因此它can't inherit its DataContext。但是有一些解决方法,即您可以明确地将 DataContext 提供给您的 DataGridColumns,其详细信息可以在Provide DataContext to DataGrid Columns 中找到。
Also, i personally like approach described here - Inheriting parent DataContext using Freezableof inheriting using Freezableclass.
此外,我个人喜欢这里描述的方法 -使用继承使用Freezable类的Freezable 继承父 DataContext。
Code from first linkin case link does not work in future -
来自first link以防链接在未来不起作用的代码-
Add this in your App.xaml.cs in App() constructor-
将此添加到您的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 forwards the DataContext from DataGrid to its columns:
OnDataContextChanged 回调只是将 DataContext 从 DataGrid 转发到其列:
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 );
}
}
}

