【发布时间】:2016-03-08 03:48:53
【问题描述】:
我有一个DataGrid 并想将Header-property 绑定到我的窗口DataContext 的属性,但我没有让它工作。
绑定可能很痛苦,因为(对我来说)在简单地使用Binding 时,永远不清楚this 具有哪个上下文。
我知道Binding={} 中的“上下文”是DataGrids ItemsSource 的单个元素。但是Header={Binding ???} 的“上下文”是什么?
我已经试过了:
Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource Self}}
Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource TemplatedParent}}
Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyWindow}}}
Header="{Binding Path=DataContext.MyProperty, ElementName=MyWindowName}
我尝试了使用和不使用 Path,但没有任何效果。
例如,使用带有ElementName 的最后一个我得到以下绑定异常:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.MyProperty; DataItem=null; target element is 'DataGridTextColumn' (HashCode=51072575); target property is 'Header' (type 'Object')
是否有任何工具可以在运行时检查/更改绑定?甚至想知道当前的“上下文”是什么?
注意:DataGrid 在 Mahapps.Flyout 内(不确定这是否有什么要说的)。
【问题讨论】:
-
我使用 Snoop 在运行时查看我的 DataContext 和绑定错误
标签: c# wpf xaml binding datagrid