为多个控件提供一个数据触发器
我希望在 window.resources 中有一个 Style DataTrigger,可用于多个扩展器。 DataTrigger 绑定到我的 ViewModel 内的枚举值,并且基于枚举值,我希望折叠正确的扩展器。例如:如果枚举值设置为“A”,那么我只希望与类型“A”关联的扩展器可见,其余扩展器折叠。
我在想这样的事情:
<Style TargetType="{x:Type Expander}">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="A">
// In here i would set the expander associated w/ "A" to Visible
// and have the rest of the expanders collapsed. Since TargetName is
// not allowed within a "Setter" property of a style, I am not sure on how to accomplish this.
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="B">
// Same concept as above
</DataTrigger>
</Style.Triggers>
</Style>
I want to have one Style DataTrigger inside my window.resources that can be used for multiple expanders. The DataTrigger is bound to an enum value inside my ViewModel, and based on the enum value, I want the the correct expanders to be collapsed. For example: If the enum value is set to "A" then I want only the expander associated with type "A" to be visible, and the rest of the expanders to become collapsed.
I was thinking of something like this:
<Style TargetType="{x:Type Expander}">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="A">
// In here i would set the expander associated w/ "A" to Visible
// and have the rest of the expanders collapsed. Since TargetName is
// not allowed within a "Setter" property of a style, I am not sure on how to accomplish this.
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="B">
// Same concept as above
</DataTrigger>
</Style.Triggers>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您可以为每个具体 Expander 类手动设置样式,以避免触发代码重复,您可以使用 Style.BasedOn 构建依赖样式的层次结构:
I believe you would be able manually setup Styles for each concrete Expander class, to avoid Trigger code duplication you can use Style.BasedOn to build a hierarchy of dependent styles:
我能够使用导致每个控件“折叠”的静态资源来解决此问题(使用多个触发器),然后在每个控件内具有单独的“DataTriggers”,其中关联的枚举值导致可见性为“可见”。
见下文(我在本例中使用画布):
I was able to solve this (using multiple triggers) with a static resource that caused each Control to 'Collapse' and then having separate 'DataTriggers' inside each Control where the associated enum value causes the visibility to be 'Visible'.
See below (I used canvas in this example):