Mat*_*att 4 .net wpf datacontext xaml binding
财产Foo在我的DataContext
ViewModel {
Visibility Foo;
}
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何进入Foo内部Column.在这种情况下,我认为它可能正在寻找Foo任何绑定到的对象DataGrid ItemsSource
<DataGrid Visibility="{Binding Foo}"> // works
<DataGrid.Columns>
<DataGridTextColumn Visibility="{Binding Foo}" /> // fails
Run Code Online (Sandbox Code Playgroud)
我试过了
Binding="{Binding DataContext.Foo}"
Run Code Online (Sandbox Code Playgroud)
还有很多带RelativeSource标签的东西.
另外,有没有办法查看和选择要从GUI绑定的属性?
编辑:事实证明,列本身不是FrameworkElements,因此无法找到DataGrid祖先.但是,您可以在下面的答案中使用该技术将Column的CellTemplate的属性绑定到DataContext.
这应该工作:
<DataGridTextColumn Visibility="{Binding Path=DataContext.Foo, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
Run Code Online (Sandbox Code Playgroud)
你正确绑定到当前项的列是正确的 - 这就是你需要RelativeSource用来获取DataGrid,然后访问它的Foo属性的原因DataContext.
至于选择要绑定的属性,WPF设计器的属性面板和可视化工作室插件(如Resharper)可以提供帮助,但最终它们在除了简单绑定之外的任何其他方面都做得不是很好,所以你还剩下什么与你自己和你对正在发生的事情的理解.