WPF在资源字典中引用样式并使用触发器
我在资源字典中定义了一个 Style
,适用于 所有ComboBox
控件。在 ComboBox
控件中,我像这样引用样式:
Style="{DynamicResource MyComboBoxStyle}"
这工作正常。
我希望能够向某些 ComboBox
控件添加一些触发器。
使用作为动态资源引用的 Style
而又仍然能够将 Trigger
添加到某些 ComboBox
控件的好方法是什么?
I have a Style
defined in a resource dictionary that applies to
all ComboBox
controls. Within the ComboBox
control, I reference the style like so:
Style="{DynamicResource MyComboBoxStyle}"
This works ok.
I want to be able to add some triggers to some of the ComboBox
controls.
What is a good way to use the Style
referenced as a dynamic resource yet still be able to add Trigger
s to some of the ComboBox
controls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
重新阅读问题后,我意识到这并不是OP所问的问题。我可以删除它,但也许它对偶然发现这个问题的人有用。
下面是一个示例,其中包含定义模板和触发器的 xaml 资源字典,以及引用该资源并应用样式的窗口。
它可能会帮助某人研究使用模板和触发器:
我的资源名为“Style1.xaml”
我的主窗口代码 xaml:
Update:
After re-reading the question, I realize this isn't exactly what the OP was asking about. I could delete this, but perhaps it will be useful to someone who stumbles upon this question.
Here's an example, with a xaml resource dictionary defining the template and the triggers, along with a window that references that resource and applies the style.
It may help someone looking into using templates and triggers:
My resource named "Style1.xaml"
My MainWindow Code xaml:
为要应用触发器的
ComboBox
控件创建新样式,并使用BasedOn
新样式上的属性来设置其基本样式。Create new styles for the
ComboBox
controls that you want to apply triggers to, and use theBasedOn
property on the new style to set their base style.