c# wpf MultiTrigger 简单试验

时间:2024-04-07 10:39:21

1.概要

<Style.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsChecked" Value="true"/>
            <Condition Property="Content" Value="正如我悄悄的来"/>
        </MultiTrigger.Conditions>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Foreground" Value="Orange"/>
    </MultiTrigger>
</Style.Triggers>

2.代码

<Window x:Class="WpfApp2.Window11"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="Window11" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="CheckBox">
            <!--Triggers不是Style的内容属性,<Style.Triggers>...</Style.Triggers>这层标签不能省略-->
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsChecked" Value="true"/>
                        <Condition Property="Content" Value="正如我悄悄的来"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="FontSize" Value="20"/>
                    <Setter Property="Foreground" Value="Orange"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <CheckBox Content="悄悄的我走了" Margin="5"/>
        <CheckBox Content="正如我悄悄的来" Margin="5,0"/>
        <CheckBox Content="我挥一挥衣袖" Margin="5"/>
        <CheckBox Content="不带走一片云彩" Margin="5,0"/>
    </StackPanel>
</Window>

3.试验结果