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?
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"
<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>
My MainWindow Code 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>
Create new styles for the ComboBox
controls that you want to apply triggers to, and use the BasedOn
property on the new style to set their base style.
精彩评论