问题描述
我的问题是:我不知道如何切换 WPF 网格列的可见性.假设以下 XAML 标记:
My problem is: I can't find out how to toggle the visibility of my WPF grid column. Assume following XAML markup:
<Grid x:Name="myGrid">
<Grid.RowDefinitions>
<RowDefinition x:Name="Row1" />
<RowDefinition x:Name="Row2" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="Column1" />
<ColumnDefinition x:Name="Column2" />
</Grid.ColumnDefinitions>
</Grid>
在网格中填充了一些控件等.现在我想从我的 C# 代码中动态隐藏单个列.我尝试通过将列的定义宽度设置为零来实现这一点,例如Column1.Width = 0
.这有效,但我不太喜欢这个解决方案 - 真的没有更好的方法吗?
Aferwards the grid is filled with some controls etc. Now I want to hide a single column dynamically out of my C# code. I've tried achieving this by setting the the column's definition width to zero, e.g. Column1.Width = 0
. This works, but I don't really like this solution - is there really no better way?
我正在寻找类似 myGrid.Columns[0].Visibility = COLLAPSED
或 Column1.Visibility = HIDDEN
的东西.我就是找不到类似的东西 - 有什么想法吗?
I'm looking for something like myGrid.Columns[0].Visibility = COLLAPSED
or Column1.Visibility = HIDDEN
. I just can't find something like that - any ideas?
推荐答案
<ColumnDefinition>
<ColumnDefinition.Style>
<Style TargetType="ColumnDefinition">
<Setter Property="Width" Value="*" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsColumnVisible}" Value="False">
<Setter Property="Width" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</ColumnDefinition.Style>
</ColumnDefinition>
请在您的 ViewModel 中实现 INotifyPropertyChanged
Please do implement INotifyPropertyChanged in your ViewModel
这篇关于从 C# 代码动态切换 WPF 网格列的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!