C# · 12月 29, 2021

C#/ WPF:使GridViewColumn Visible = false?

有没有人知道是否有一个选项可以隐藏一个GridViewColumn这样: <ListView.View> <GridView> <GridViewColumn Header=”Test” IsVisible=”{Binding Path=ColumnIsVisible}” /> </GridView><ListView.View>

非常感谢!

编辑:为了清楚起见

不幸的是,没有“IsVisible”属性.我正在寻找一种创造方式.

编辑:基于反馈的解决方案如下所示:

<GridViewColumn DisplayMemberBinding=”{Binding Path=OptionColumn1Text}” Width=”{Binding Path=SelectedEntitiy.OptionColumn1Width}”> <GridViewColumnHeader Content=”{Binding Path=SelectedEntitiy.OptionColumn1Header}” IsEnabled=”{Binding Path=SelectedEntitiy.OptionColumn1Width,Converter={StaticResource widthToBool}}” /></GridViewColumn>public class WidthToBooleanConverter : IValueConverter{ public object Convert(object value,Type targetType,object parameter,CultureInfo culture) { return (int)value > 0; } public object ConvertBack(object value,CultureInfo culture) { throw new NotImplementedException(); }}

谢谢大家!
干杯

解决方法 编辑:反映修改后的问题.

如何创建0宽度列?将一个布尔值写入宽度IValueConverter,将ColumnIsVisible作为ConverterParmeter?

public class BooleanToWidthConverter : IValueConverter { public object Convert(object value,CultureInfo culture){ return ((bool) parameter)? value : 0; } public object ConvertBack(object value,CultureInfo culture){ throw new NotImplementedException(); } }

就像是:

<ListView .. > <ListView.Resources> <BooleanToWidthConverter x:Key=”boolToWidth” /> </ListView.Resources> <ListView.View> <GridView> <GridViewColumn Header=”Test” Width= “{Binding Path=ColumnWidth,Converter={StaticResource boolToVis},ConverterParameter=ColumnIsVisible}” /> </GridView> <ListView.View>