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, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Is there an option to hide a GridViewColumn somehow like this:
<ListView.View>
<GridView>
<GridViewColumn Header="Test" IsVisible="{Binding Path=ColumnIsVisible}" />
</GridView>
<ListView.View>
Edit: For clarity
Unfortunately, there is no "IsVisible" Property. I'm looking for a way to create that.
Edit: The solution based on the feedback looks like:
<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, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
编辑:反映修改后的问题。
创建一个 0 宽度的列怎么样?将布尔值写入宽度 IValueConverter,将 ColumnIsVisible 作为 ConverterParmeter?
像这样的东西:
Edit: Reflecting the modified question.
What about creating a 0 width column? Write a Boolean to Width IValueConverter, that takes a ColumnIsVisible as the ConverterParmeter?
Something like:
这里是另一种基于将列宽度设置为零的解决方案。我稍微修改了一下。现在它的工作方式如下:
以下是代码。
XAML:
BooleanToVisibilityConverter:
附加行为GridViewBehaviors.CollapseableColumn:
Here is another solution based on setting the column's width to zero. I have modified it a little. It now works like this:
Here is the code.
XAML:
BooleanToVisibilityConverter:
Attached behavior GridViewBehaviors.CollapseableColumn:
您可以以编程方式删除它:
如果您知道标头的名称:
或者如果您知道标头的索引(如果可以更改标头名称,例如,如果您有本地化的应用程序,那就更好了):
You could remove it programmatically:
If you know the name of the header:
Or if you know the index of the header (it is better if the header name can be changed for example if you have localized application):
一种更简单的方法仍然使用将列宽度设置为零的概念,但没有使用 IValueConverter 的副作用(用户仍然可以将列拖得更宽),即创建一个新的 getter /setter 根据您的
ColumnIsVisible
变量返回宽度,然后绑定到该宽度:使您的绑定为 TwoWay,如果用户尝试将列拖得更宽,
OnPropertyChanged
将被调用并将宽度重置为 0。不过,您可能必须使用绑定代理来进行绑定。当 ColumnIsVisible 更新时,还要添加对OnPropertyChanged("ColumnWidth")
的调用:)One simpler approach, that still uses the concept of setting the columns width to zero but does not have the side effects of using a
IValueConverter
(the user can still drag the column wider) is to create a new getter/setter that returns a width based on yourColumnIsVisible
variable and then bind to that:Make your bindings TwoWay and if the user attempts to drag the column wider
OnPropertyChanged
will be called and reset the width to 0. You might have to use a binding proxy though for your binding. Also add a call toOnPropertyChanged("ColumnWidth")
when ever ColumnIsVisible is updated :)我已将列设置为
width="0"
。现在该列看起来不可见。但不知道会不会影响其他什么。
这可能是一个虚拟的解决方案,但目前它有效。
I've set the column the
width="0"
.Now the column looks like its not visible. But i do not know if it will affect anything else.
It might be a dummy solution but for now it works.
我选择创建一个新的类来保存GridView的状态,然后在状态改变时手动更新原来列表的状态,相当于在不可见的时候将其移除,而不是隐藏它。
I chose to create a new class to save the state of the GridView, and then manually update the state of the original list when the state changes, which is equivalent to removing it when it is invisible, rather than hiding it.
问题,请使用
如果 Thumb.DragDelta 可以解决我在列表视图中使用它的
Use if Thumb.DragDelta may solve the problem
I use it in listview as