简体   繁体   中英

WPF reference style in resource dictionary and use triggers

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>

为要应用触发器的ComboBox控件创建新样式,并使用新样式的BasedOn属性设置其基本样式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM