在 WPF 中绑定 DataGridTextColumn 可见性属性 [英] Bind DataGridTextColumn Visibility Property in WPF
问题描述
我有一个数据网格,它的 ItemsSource
绑定到一个 CollectionViewSource
.
在每一列中,我指定绑定的 Path
属性以获取要显示的特定信息.
I have a datagrid whose ItemsSource
binds to a CollectionViewSource
.
In each column I specify the Path
property of the binding to get the specific information to display.
如果用户需要更多信息,我想做的是使用复选框切换某些列.为此,我需要将可见性属性绑定到复选框的值(使用转换器),但我很确定列的数据上下文会干扰绑定.
What I'd like to do is toggle some of the columns with a checkbox if the user wants more info. To do this, I need to bind the visibility property to the value of the checkbox (with a converter) but I'm pretty sure the data context of the column is interfering with the binding.
<DataGrid ItemsSource="{Binding Source={StaticResource cvs}}" ....>
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"
Visibility="{Binding IsChecked,
ElementName=IncludeFullHist,
Converter={StaticResource boolItemsConverter}}"/>
</DataGrid.Columns>
</DataGrid>
我的视图模型中也需要复选框,所以我将它的 IsChecked
属性绑定到我的 ViewModel 上的属性
I need the checkbox in my viewmodel as well, so I have its IsChecked
property bound to a property on my ViewModel
<CheckBox x:Name="IncludeFullHist" IsChecked="{Binding Path=ManagerFullHist }" />
<小时>
对于页面中的其他元素,我已经能够使用以下两种方法之一连接可见性绑定,但是当我将它们复制到数据网格中时,这两种方法似乎都不起作用:
For other elements in my page, I've been able to hook up visibility bindings with either of the two following methods, but neither seem to work when I copy them over into the datagrid:
<TextBlock DockPanel.Dock="Left" Text=" Visible 2 "
Visibility="{Binding Path=DataContext.ManagerFullHist,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}},
Converter={StaticResource boolItemsConverter}}"/>
<TextBlock DockPanel.Dock="Left" Text=" Visible 3 "
Visibility="{Binding Path=ManagerFullHist,
Source={StaticResource mainWinResource},
Converter={StaticResource boolItemsConverter}}"/>
关于在数据网格中解决这个问题的方法有什么建议吗?
如果我遗漏了任何可能有用的代码,请告诉我.
Any suggestions on ways that I can solve this in the datagrid?
Please let me know if I've omitted any code that could be potentially helpful.
推荐答案
DataGridColumn
实际上不是 VisualTree
的一部分,因此类上的绑定无法找到它们的来源
The DataGridColumn
is not actually part of the VisualTree
, so bindings on the class cannot find their source
您可以在 CellStyle
或 HeaderStyle
中设置诸如 Visibility
和 Width
属性之类的内容>DataGridColumn,虽然这并不完全相同.
You can set things like the Visibility
and Width
property in the CellStyle
or HeaderStyle
of the DataGridColumn
, although that isn't quite the same.
我发现的最接近解决方案的方法是创建一个 Freezable 对象,然后在 Visibility
绑定中使用该 StaticResource
.这不是一个很好的解决方案,但这是我目前唯一能找到的.
The closest I've found to a solution would be to create a Freezable object in your <DataGrid.Resources>
that stores the binding, then use that StaticResource
in the Visibility
binding. It's not a pretty solution, but it's the only one I can find at this time.
您可以查看它的示例 这里
<DataGrid.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding IsChecked,
ElementName=IncludeFullHist,
Converter={StaticResource boolItemsConverter}}" />
</DataGrid.Resources>
<DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"
Visibility="{Binding Path=Data, Source={StaticResource proxy}}"/>
这篇关于在 WPF 中绑定 DataGridTextColumn 可见性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!