WPF:如何使用 XAML 隐藏 GridViewColumn?
我在 App.xaml 中有以下对象
<Application.Resources>
<ResourceDictionary>
<GridView x:Key="myGridView" x:Shared="false">
<GridViewColumn Header="Created" DisplayMemberBinding="{Binding Path=Created}"/>
... more code ...
,并且我在多个地方使用此网格视图。 示例:
<ListView x:Name="detailList" View="{StaticResource myGridView}" ...>
在其中一种用法(例如上面的detailList)中,我想隐藏Created 列,可能使用XAML 吗?
有任何想法吗?
I have the following object in App.xaml
<Application.Resources>
<ResourceDictionary>
<GridView x:Key="myGridView" x:Shared="false">
<GridViewColumn Header="Created" DisplayMemberBinding="{Binding Path=Created}"/>
... more code ...
And I use this grid view in multiple places. Example:
<ListView x:Name="detailList" View="{StaticResource myGridView}" ...>
In one of the usages (such as detailList above), I'd like to hide the Created column, possibly using XAML?
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
实际上,我发现最简单的解决方案是通过附加属性:
然后,它可以这样使用:
Actually, I find the easiest solution is via attached properties:
Then, it can be used as so:
基于 Ben McMillan 的答案,但支持动态更改可见属性。
我通过删除 IsEnabled 属性进一步简化了他的解决方案。
Based on Ben McMillan's answer, but supports dynamic changing of visible property.
I've simplified his solution further by removing the IsEnabled property.
您最好的选择可能是通过继承 GridView 类、添加所需的列并公开有意义的属性来显示/隐藏特定列来创建自定义控件。 您的自定义 GridView 类可能如下所示:
然后您可以简单地从 XAML 设置该属性,如下例所示:
或者,您可以将“CustomGridView.ShowOptionalColumn”设置为 DependencyProperty能够将其用作绑定目标。
You best bet is probably to create a custom control by inheriting from the GridView class, adding the required columns, and exposing a meaningful property to show/hide a particular column. Your custom GridView class could look like this:
Then you can simply set that property from XAML like in this example:
Optionally, you could make the 'CustomGridView.ShowOptionalColumn' a DependencyProperty to be able to use it as a binding target.
摘自此处
Taken from here
我有一个比使用附加行为更简单的解决方案。
您所要做的就是将 GridViewColumn 的 Width 属性绑定到 ViewModel 上的布尔值。 然后创建一个简单的转换器,例如 BooleanToWidthConverter,它接受一个布尔值并返回一个双精度值,如果为假则返回零,如果为真则返回 x 宽度。
我希望这对您有所帮助并使您的生活更轻松。
XAML:
转换器:
I have a much simpler solution than using an Attached Behavior.
All you have to do is bind the Width Property of the GridViewColumn to a boolean on your ViewModel. Then create a simple Converter like BooleanToWidthConverter that takes a boolean and returns a double, zero if its false, x width if its true.
I hope this helps and makes your life easier.
XAML:
Converter:
这是我的代码,它在我的项目中运行得很好。 如果您不想添加一些外部代码。
This is my code , it works very well in my project. if you don't like to add some external code.
我建议在父级上使用自定义属性(或劫持现有属性),然后在 gridviewcolumnheader 上使用自定义样式来引用该祖先属性。 像这样:
I'd suggest using a custom property (or hijacking an existing one) on the parent and then using a custom style on the gridviewcolumnheader to reference that ancestor property. Like this:
在我编写的一个小实用程序中,我有一个列表视图,用户可以在其中隐藏/显示一些列。 列上没有 Visibility 属性,因此我决定将隐藏列宽度设置为零。 并不理想,因为用户仍然可以调整它们的大小并使它们再次可见。
无论如何,为了做到这一点,我使用了:
IsThreadIdShown
绑定到工具栏上的复选框。多值转换器为:
In a small utility I wrote, I have a list view where the user can hide/show some columns. There is no Visibility property on the columns, so I decided to set the hidden columns width to zero. Not ideal, as the user can still resize them and make them visible again.
Anyway, to do this, I used:
IsThreadIdShown
is bound to a check box on the toolbar.And the multi-value converter is:
这对我有用
需要在标题和内容上绑定 Visibility
在本例中,它位于末尾,因此我不必担心宽度
但用户没有获得 UI 挂钩来重置宽度,因此如果将宽度设置为零,它就会消失
This works for me
Need to bind the Visibility on both the header and the content
In this case it is at the end so I don't worry about the Width
BUT the user does not get a UI hook to reset the width so if you set the width to zero it is gone
类 BooleanToWidthConverter :IValueConverter
{
不幸的是,GridViewColumn 中没有“IsVisible”属性
但是,我有一个解决方案可以通过简单而完整的方式解决这个问题:
上面的代码示例:
class BooleanToWidthConverter :IValueConverter
{
Unfortunately, there is no "IsVisible" Property in GridViewColumn
But, I have a solution can solve this problem by simple and complete way:
Code sample above:
有点晚了,但我认为这可能仍然有用:
根据 A.Pissicat、Surfen 和 Ben McMillan 的代码(和注释),我更新了
GridViewColumnVisibilityManager
如下:如果您愿意隐藏列时使用特定的
HeaderContainerStyle
Style
而不是null
,然后替换:gridViewColumn.HeaderContainerStyle = columnValues.Style;
code>with
gridViewColumn.HeaderContainerStyle = Application.Current.TryFindResource(@"disabledColumn") as Style;
并将
@"disabledColumn"
更改为您想要使用的任何名称。A bit late, but I thought this might still be useful:
Based on the code (and comments) of A.Pissicat, Surfen and Ben McMillan, I've updated
GridViewColumnVisibilityManager
as follows:If you wish to use a particular
HeaderContainerStyle
Style
instead ofnull
when hiding the column, then replace:gridViewColumn.HeaderContainerStyle = columnValues.Style;
with
gridViewColumn.HeaderContainerStyle = Application.Current.TryFindResource(@"disabledColumn") as Style;
and change
@"disabledColumn"
to whatever name you want to use.