WPF中DataGridColumn的绑定可见性 [英] Binding Visibility for DataGridColumn in WPF
问题描述
如何通过绑定隐藏WPF DataGrid
中的列?
How can I hide a column in a WPF DataGrid
through a Binding?
这就是我所做的:
<DataGridTextColumn Header="Column header"
Binding="{Binding ColumnValue}"
Width="100"
ElementStyle="{StaticResource DataGridRightAlign}"
Visibility="{Binding MyColumnVisibility}" />
这是我得到的(列除了可见):
And this is what I got (besides the column still visible):
System.Windows.Data错误:2:找不到针对目标元素的FrameworkElement或FrameworkContentElement。 BindingExpression:Path = MyColumnVisibility; DataItem = null; target元素为'DataGridTextColumn'(HashCode = 1460142);目标属性是可见性(类型可见性)
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=MyColumnVisibility; DataItem=null; target element is 'DataGridTextColumn' (HashCode=1460142); target property is 'Visibility' (type 'Visibility')
不知道什么是治疗手段。总统先生有一些总统在我的窗口里,决定什么工作,什么不是?或者我必须投票吗?
Don't know what governing means. Is there some Mr. President somewhere in my window that decides what works and what not? Or would I have to vote for something?
在搜索网络解决方案的同时,我发现十几页具有前瞻性的标题,但完全无关或不可复制的内容。所以这似乎是关于这个问题的第一个问题。任何想法?
While searching the web for solutions, I found like a dozen pages with a promising title but totally unrelated or unreproducible contents. So this seems to be the first question about the problem. Any idea?
推荐答案
首先 DataGridTextColumn
或任何其他支持的dataGrid列不在于 DataGrid
的视觉树。因此,默认情况下,不会继承 DataGrid
的 DataContext
。但是,它适用于绑定
DP,而不适用于DataGridColumn上的其他DP。
First of all DataGridTextColumn
or any other supported dataGrid columns doesn't lie in Visual tree of DataGrid
. Hence, by default it doesn't inherit DataContext
of DataGrid
. But, it works for Binding
DP only and for not other DP's on DataGridColumn.
由于它们不在同一个VisualTree中,所以任何尝试使用 RelativeSource
的DataContext将无法正常工作,因为DataGrid将无法遍历DataGrid。
Since, they doesn't lie in same VisualTree so any try to get DataContext using RelativeSource
won't work as well because DataGrid won't able to traverse up to DataGrid.
有两种方法可以实现:
第一个使用 Freezable
class - Freezable
对象可以继承DataContext,即使它们不在视觉或逻辑树。
First using Freezable
class - Freezable
objects can inherit the DataContext even when they’re not in the visual or logical tree. So, we can take advantage of that to our use.
首先创建从 Freezable继承的类
和数据
DP可以用来绑定在XAML中:
First create class inheriting from Freezable
and Data
DP which we can use to bind in XAML:
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); }
}
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object),
typeof(BindingProxy));
}
现在,在DataGrid资源中添加一个实例,以便它可以继承DataGrid的DataContext然后可以与其Data DP绑定:
Now, add an instance of it in DataGrid resources so that it can inherit DataGrid's DataContext and then can bind with its Data DP:
<DataGrid>
<DataGrid.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Visibility="{Binding Data.MyColumnVisibility,
Source={StaticResource proxy}}"/>
</DataGrid.Columns>
</DataGrid>
第二个可以使用 ElementName
或 x:Reference
引用XAML中的任何UI元素。但是 ElementName
只能在同一个可视树中工作,而x:Reference不具有这样的约束。
Second, you can refer to any UI element in XAML using ElementName
or x:Reference
. But ElementName
works only in same visual tree whereas x:Reference doesn't have such constraint.
所以,我们也可以使用它,我们的优势。在可见性设置为折叠的XAML中创建虚拟 FrameworkElement
。 FrameworkElement将从它的父容器继承DataContext,可以是Window或UserControl。
So, we can use that as well to our advantage. Create dummy FrameworkElement
in XAML with Visibility set to collapsed. FrameworkElement will inherit DataContext from it's parent container which can be Window or UserControl.
可以在DataGrid中使用它:
And can use that in DataGrid:
<FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/>
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Test"
Binding="{Binding Name}"
Visibility="{Binding DataContext.IsEnable,
Source={x:Reference dummyElement}}"/>
</DataGrid.Columns>
</DataGrid>
这篇关于WPF中DataGridColumn的绑定可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!