WPF link style in resource dictionary and use of triggers

I have Style, defined in the resource dictionary, which applies to all controls ComboBox. Inside the control, ComboBoxI reference the style as follows:

Style="{DynamicResource MyComboBoxStyle}"

This is working fine.

I want to add some triggers to some controls ComboBox.

What is a good use case Stylereferenced as a dynamic resource, but can still be added Triggerto some controls ComboBox?

+3
source share
2 answers

: , , , . , , , -, .


, xaml, , , .

- :

"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>
+1

ComboBox, , BasedOn , .

0

Source: https://habr.com/ru/post/1741797/


All Articles