我在资源字典中定义了一个Style
,该样式适用于所有的ComboBox
控件。在ComboBox
控件内部,我可以通过以下方式引用该样式:
Style="{DynamicResource MyComboBoxStyle}"
这个做法还算不错。
我想要在某些ComboBox
控件上添加一些Trigger
。
如何使用作为动态资源引用的Style
,同时仍然能够向某些ComboBox
控件添加Trigger
?
我在资源字典中定义了一个Style
,该样式适用于所有的ComboBox
控件。在ComboBox
控件内部,我可以通过以下方式引用该样式:
Style="{DynamicResource MyComboBoxStyle}"
这个做法还算不错。
我想要在某些ComboBox
控件上添加一些Trigger
。
如何使用作为动态资源引用的Style
,同时仍然能够向某些ComboBox
控件添加Trigger
?
更新: 重新阅读问题后,我意识到这并不完全是OP所询问的内容。我可以删除它,但或许对那些偶然发现这个问题的人有用。
这可能会帮助那些想使用模板和触发器的人:
我的资源名为“Style1.xaml”
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="TonyTemplate" TargetType="Button">
<Border Name="Border"
BorderBrush="Orange"
BorderThickness="3"
CornerRadius="2"
Background="Ivory"
TextBlock.Foreground="Black">
<Grid>
<ContentPresenter RecognizesAccessKey="True"
Margin="{TemplateBinding Padding}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="Yellow" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Border" Property="Background" Value="Chartreuse" />
<Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
我的MainWindow代码xaml:<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Width="100" Height="50"
Template="{StaticResource TonyTemplate}"
Content="Click me"/>
</Grid>
</Window>
为您想要应用触发器的ComboBox
控件创建新样式,并在新样式上使用BasedOn
属性来设置它们的基础样式。