WPF通过绑定隐藏DataGridColumn [英] WPF Hide DataGridColumn via a binding
问题描述
由于某种原因,我无法隐藏WPF Toolkit的DataGridColumn.我正在尝试执行以下操作:
For some reason I can't hide WPF Toolkit's DataGridColumn. I am trying to do the following:
<dg:DataGridTemplateColumn Header="Item Description" Visibility="{Binding IsReadOnly}">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ItemDescription}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
这不起作用,因为它正在寻找ItemSource上的IsReadOnly属性(而不是当前类的属性). 如果将此添加为实现INoifyPropertyChanged的ItemSource类的属性,则它仍不会隐藏该列.有没有解决的办法?我希望在单击按钮更改IsReadOnly属性时隐藏该列.
This doesn't work, since it's looking for a IsReadOnly property on the ItemSource (not a property of the current class). If add this as a property of the ItemSource class that implements INoifyPropertyChanged, it still doesn't hide the column. Is there a way around this? I want the column to hid when a button click changes IsReadOnly property.
假设IsReadOnly返回一个Visibility值,并且是一个依赖项属性
Assume IsReadOnly returns a Visibility value and is a dependency property
我完全被困住了,我将非常感谢您的帮助!非常感谢!
I am completely stuck, I would really appreciate the help! Thanks a lot!
推荐答案
如果要绑定到DataGridColumn
的IsReadOnly
属性,只需在绑定(和转换器)中添加RelativeSource
:
If you want to bind to the DataGridColumn
's IsReadOnly
property, just add a RelativeSource
to the Binding (and a converter):
<BooleanToVisibilityConverter x:Key="boolToVis" />
...
<dg:DataGridTemplateColumn Header="Item Description" Visibility="{Binding IsReadOnly, RelativeSource={RelativeSource Self}, Converter={StaticResource boolToVis}}">
此外,它看起来像此StackOverflow问题可能与您的问题有关.
Also, it looks like this StackOverflow question might be related to your problem.
这篇关于WPF通过绑定隐藏DataGridColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!