如何在不覆盖现有样式的情况下向WPF自定义控件添加触发器? [英] How to add a trigger to a WPF custom control without overriding the existing style?
问题描述
我正在创建一个简单的自定义控件,该控件从切换按钮扩展而来,允许用户直接在XAML中指定选中和未选中的内容。效果不错,但是它基于触发器,除了样式之外,我不知道如何定义触发器。如果我定义了样式,那么我将失去自定义控件之外的所有设置。
I am creating a simple custom control extending from toggle button that allows the user to specify checked and unchecked content directly in XAML. It works well but it is based on a trigger, and I don't know how to define the trigger except in a style. If I define the style, then I lose anything set outside of the custom control.
我想做的就是将触发器附加到任何现有样式中设置在控件上的其他位置。
What I would like to be able to do is just append this trigger to any existing style set elsewhere on the control.
这是样式/触发器的XAML。
Here's the XAML for the style/trigger.
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Self}, Path=UncheckedContent}" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content"
Value="{Binding RelativeSource={RelativeSource Self}, Path=CheckedContent}" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
我尝试通过默认类型的BasedOn继承样式,但是如果自定义控件具有由其父级设置的显式样式。我也考虑过EventTriggers,但我不相信会有一个事件来初始化控件。
I tried inheriting the style via the BasedOn with a default type but it won't work if the custom control has an explicit style set by its parent. I also considered EventTriggers but I do not believe there would be an event to initialize the control.
感谢任何人都可以提供的帮助。 :)
Thanks for any help anyone can offer. :)
推荐答案
只需在此处清除术语:用户控件是一个控件从UserControl类派生的。如果我理解正确,那么您可以从ToggleButton派生来添加UncheckedContent和CheckedContent属性。在这种情况下,您已经创建了一个自定义控件。如果我们就通用术语达成共识,总是比较容易遵循:)
Just to clear things up on the terminology here: A user control is a control that derives from the UserControl class. If I understood you right you derived from ToggleButton to add the UncheckedContent and CheckedContent properties. In that case you have created a custom control. It's always easier to follow if we agree on common terminology :)
据我所知,您无法在XAML中进行此类通用样式继承。您始终必须明确指定另一种样式所基于的样式。您的样式可以基于ToggleButton的默认样式,也可以基于其他特定样式。如果您无法建立一个尊重这一点的样式继承链,那么这种方法将行不通。
As far as I know you can not do such a generic style inheritance in XAML. You always have to specify explicitly what style a another style is based upon. Your style can either be based on the default style for ToggleButton or on a specific other style. If you can't build a style inheritance chain that respects that, this approach won't work.
但是由于您具有自定义控件,因此您不能为它编写基于这样的默认切换按钮样式的默认样式吗?
But since you have a custom control, couldn't you write a default style for it that is based on the default toggle button style like this?
<Style TargetType="{x:Type CustomToggleButton}"
BasedOn="{StaticResource {x:Type ToggleButton}}">
然后,当您将显式样式应用于切换按钮时,您将指定其基于默认样式
Then whenever you apply an explicit style to a toggle button you would specify that it is based on the default toggle button style.
此外,您还可以在Themes\Generic.xaml中为新的切换按钮编写一个(默认)控件模板,其中包含上述触发器。在Blend中,您可以获取切换按钮的默认模板的副本(编辑模板->编辑副本),从而可以确保切换按钮的外观与普通切换按钮的外观完全相同。然后将上面的触发器合并到该模板中。
Also you could write a (default) control template for your new toggle button in Themes\Generic.xaml that contains the above triggers. In blend you can get a copy of the default template for toggle button ("Edit Template"->"Edit a Copy") so you can make sure that your toggle button looks exactly like the normal toggle button. Then incorporate the triggers above into that template.
BTW:您不必创建新控件就可以添加新属性。您可以使用附加的属性。可以像常规属性一样从XAML使用它们。
BTW: you do not have to create a new control just to add new properties. You can add new properties to an existing control using attached properties. They can be used from XAML just like normal properties.
这篇关于如何在不覆盖现有样式的情况下向WPF自定义控件添加触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!