XAML 在多个选项之间切换可见性
在我的 WPF 应用程序的一个屏幕上,我列出了一系列有用的提示来向用户展示。每个提示消息都包含复杂的标记,因此我想将消息定义保留在 XAML 中。我一次应该只显示其中一个提示。
如何根据我的枚举切换这些消息的可见性?
我当前正在做的事情如下所示:
<ContentControl Grid.Row="1">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Suggestion}" Value="AddDescription">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
<Border Style="{StaticResource SuggestBox}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Style="{StaticResource SuggestImage}"/>
<TextBlock Grid.Column="1" Style="{StaticResource SuggestMessage}">
You can add a description to this bookmark. Adding a description will make it easier to find.
<Hyperlink Command="{Binding EditCommand}">Add a description.</Hyperlink>
</TextBlock>
</Grid>
</Border>
</ContentControl>
在上面的代码中,建议是一个枚举,其值对应于应该显示的提示。我仅展示了 XAML 作为一项提示。每个附加提示都有自己的内容控件,其样式触发器绑定到建议枚举的不同值。
这看起来像很多 XAML 来支持简单的可见性切换操作。当然有更简单或更好的方法吗?
(请注意,我无法将可见性触发器直接应用于 Border 元素的原因是 Border 元素已经设置了样式。显然,WPF 不允许您组合样式。)
更新: 刚刚意识到这本质上是与 WPF Visibility of a UI element based on commoelection 和 如何隐藏/显示堆栈面板中的项目?但在不同的上下文中。这些问题的答案看起来和我的 XAML 一样丑陋。
On one of the screens in my WPF application, I have an enumerated list of helpful tips to show the user. Each of these tip messages contains complex markup, so I'd like to keep the message definitions in XAML. I should only show one of these tips at a time.
How can I toggle the visibility of these messages based on my enum?
What I'm currently doing looks like this:
<ContentControl Grid.Row="1">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Suggestion}" Value="AddDescription">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
<Border Style="{StaticResource SuggestBox}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Style="{StaticResource SuggestImage}"/>
<TextBlock Grid.Column="1" Style="{StaticResource SuggestMessage}">
You can add a description to this bookmark. Adding a description will make it easier to find.
<Hyperlink Command="{Binding EditCommand}">Add a description.</Hyperlink>
</TextBlock>
</Grid>
</Border>
</ContentControl>
In the code above, Suggestion is an enum with values that correspond to the tip that ought to be displayed. I've only shown the XAML for one tip. Each additional tip has its own content control, with its style trigger bound to a different value of the Suggestion enum.
This seems like a lot of XAML to support a simple visibility toggle operation. Surely there's an easier or better way?
(Note that the reason I can't apply the visibility trigger directly to the Border element is that the Border element already has a style set. Apparently, WPF doesn't allow you to combine styles.)
UPDATE: Just realized that this is essentially the same question as WPF Visibility of a UI element based on combo selection and How to hide/show items in a stack panel? but in a different context. The answers to those questions seem about as ugly as my XAML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用转换器:http://msdn。 microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert.aspx
像这样称呼它:
或者类似的东西,但这是一般的想法。
Try using a Converter: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert.aspx
Call it like this:
Or something similar but that's the general idea.