WPF 触发器绑定:将枚举值绑定到可见性的最佳方法是什么?

发布于 2024-09-18 23:23:39 字数 545 浏览 7 评论 0原文

我有一个用户控件(NameField)。其中我有一个包含 3 个网格的堆栈面板:StandardViewFluidViewOtherView。在代码隐藏中,我有一个名为 ViewDependencyProperty,其类型为 NameFieldView (enum)。枚举包含STANDARDFLUIDOTHER

我想我必须创建一个转换器,但我不确定这是否有必要。我基本上想让唯一可见的网格是与枚举值匹配的网格...也就是说,如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我做我的改变 2024-09-25 23:23:40

我为此使用数据触发器。它看起来像这样;

<Style TargetType="DockPanel" x:Key="ViewStyle1">
   <Setter Property="Visibility" Value="Collapsed"/>
   <Style.Triggers>
     <DataTrigger Binding="{Binding ViewStyle}" Value="ViewStyle1">
       <Setter Property="Visibility" Value="Visible"/>
     </DataTrigger>
   </Style.Triggers>
</Style>

然后,我为每种视图样式创建一个 DockPanel,只要 ViewStyle 属性发生更改,就会显示相应的视图。

I use data triggers for this. It looks something like this;

<Style TargetType="DockPanel" x:Key="ViewStyle1">
   <Setter Property="Visibility" Value="Collapsed"/>
   <Style.Triggers>
     <DataTrigger Binding="{Binding ViewStyle}" Value="ViewStyle1">
       <Setter Property="Visibility" Value="Visible"/>
     </DataTrigger>
   </Style.Triggers>
</Style>

Then I create a DockPanel for each view style, and whenever the ViewStyle property changes, the appropriate view displays.

没有伤那来痛 2024-09-25 23:23:40

与许多 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.

一身骄傲 2024-09-25 23:23:40

我会创建一个转换器。如果您在遇到此类适当的绑定问题时添加转换器,您将慢慢为您的应用程序构建一个它们的库,从而使您将来的事情变得更加容易。我将其称为 NameFieldViewToVisibilityConverter 之类的东西 - 它应该有两个方法:

public Object Convert(Object value, Type TargetType, Object param, CultureInfo Culture);
public Object ConvertBack(Object value, Type TargetType, Object param, CultureInfo Culture);

Convert 将有一个 NameFieldView 参数并返回一个 Visibility 值。
ConvertBack 将具有 Visibility 参数并返回 NameFieldView 值。

绑定看起来像这样:

<Grid Name="StandardView" Visibility="{Binding View, Converter={StaticResource NameFieldViewToVisibilityConverter}"  />

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:

public Object Convert(Object value, Type TargetType, Object param, CultureInfo Culture);
public Object ConvertBack(Object value, Type TargetType, Object param, CultureInfo Culture);

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:

<Grid Name="StandardView" Visibility="{Binding View, Converter={StaticResource NameFieldViewToVisibilityConverter}"  />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文