WPF 触发器绑定:将枚举值绑定到可见性的最佳方法是什么?
我有一个用户控件(NameField
)。其中我有一个包含 3 个网格的堆栈面板:StandardView
、FluidView
、OtherView
。在代码隐藏中,我有一个名为 View
的 DependencyProperty
,其类型为 NameFieldView
(enum
)。枚举包含STANDARD
、FLUID
、OTHER
。
我想我必须创建一个转换器,但我不确定这是否有必要。我基本上想让唯一可见的网格是与枚举值匹配的网格...也就是说,如果 View = NameFieldView.STANDARD
则名为 StandardView
的网格> 是可见的,而其他两个则不可见。
另外,我不确定这是否应该是 Grid.Resources / Style 或 Grid.Triggers 的一部分?
I have a user control (NameField
). Within it I have a stackpanel containing 3 Grids: StandardView
, FluidView
, OtherView
. Within the code-behind I have a DependencyProperty
called View
of type NameFieldView
(enum
). The enum contains STANDARD
, FLUID
, OTHER
.
I think I have to create a converter, but I'm not sure if that's necessary. I basically want to make it so that the only visible grid is the one that matches the enum value... that is, if View = NameFieldView.STANDARD
then the Grid named StandardView
is visible and the other two are not.
Also, I'm not sure if this should be part of Grid.Resources / Style
or Grid.Triggers
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我为此使用数据触发器。它看起来像这样;
然后,我为每种视图样式创建一个
DockPanel
,只要ViewStyle
属性发生更改,就会显示相应的视图。I use data triggers for this. It looks something like this;
Then I create a
DockPanel
for each view style, and whenever theViewStyle
property changes, the appropriate view displays.与许多 WPF 一样,这实际上取决于您的品味。这里有一些选择。
您可以创建三个 IValueConverter,将 View 属性的值转换为 Visibility(或使用枚举名称作为 ConverterParameter 并创建一个转换器)。
您可以创建三个名为“StandardViewIsVisible”、“FluidViewIsVisible”和“OtherViewIsVisible”的新属性,这些属性会在 View 属性更改时更新。这些属性的返回类型为 Visibility。即使您没有使用 ViewModel,这显然更像是一种“MVVM”做事方式。
您可以使用 DataTrigger 根据 View 属性的当前值设置适当的网格 Visible 或 Collapsed。
Like a lot of WPF, it really depends on your taste. Here are a few choices.
You could create three IValueConverter that converts the value of the View property to a Visibility (or use the enum name as a ConverterParameter and create one converter).
You could create three new properties called "StandardViewIsVisible", "FluidViewIsVisible", and "OtherViewIsVisible" that get updated when the View property changes. These properties would be of return type Visibility. This is decidedly more of an "MVVM" way of doing things, even if you aren't using a ViewModel.
You could use a DataTrigger that sets the appropriate grid Visible or Collapsed based on the current value of the View property.
我会创建一个转换器。如果您在遇到此类适当的绑定问题时添加转换器,您将慢慢为您的应用程序构建一个它们的库,从而使您将来的事情变得更加容易。我将其称为 NameFieldViewToVisibilityConverter 之类的东西 - 它应该有两个方法:
Convert 将有一个 NameFieldView 参数并返回一个 Visibility 值。
ConvertBack 将具有 Visibility 参数并返回 NameFieldView 值。
绑定看起来像这样:
I would create a converter. If you add a converter whenever you have an appropriate binding problem like this, you will slowly build a library of them for your application, making things much easier for yourself in the future. I would call it something like NameFieldViewToVisibilityConverter - it should have two methods:
Convert will have a NameFieldView param and returns a Visibility value.
ConvertBack will have a Visibility param and returns a NameFieldView value.
The bindings would look like this: