Java教程

wpf之触发器

本文主要是介绍wpf之触发器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 示例:

<Window x:Class="MyWpf.MainWindow"
        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:MyWpf"
        mc:Ignorable="d" 
        Title="MainWindow" Height="250" Width="500">
    <Window.Resources>
        <!--表示这个样式只针对于Button标签,这个样式的key值是BaseStyle,
        -->
        <Style TargetType="TextBox" x:Key="BaseStyle">
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="HorizontalAlignment" Value="Left"></Setter>
            <Setter Property="VerticalAlignment" Value="Top"></Setter>
            <Setter Property="Width" Value="75"></Setter>

            <Style.Triggers>
                <!--如果鼠标移动到button上方,对当前按钮样式进行更改-->
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="FontSize" Value="22"></Setter>
                    <Setter Property="Foreground" Value="Blue"></Setter>
                </Trigger>
                <!--多条件出发-->
                <MultiTrigger>
                    <!--只有鼠标在按钮上方并且选中才会触发事件-->
                    <MultiTrigger.Conditions>
                        <Condition Property="IsFocused" Value="True"></Condition>
                        <Condition Property="IsMouseOver" Value="True"></Condition>
                    </MultiTrigger.Conditions>
                    <!--触发事件之后的样式-->
                    <MultiTrigger.Setters>
                        <Setter Property="FontSize" Value="22"></Setter>
                        <Setter Property="Foreground" Value="Blue"></Setter>
                    </MultiTrigger.Setters>
                </MultiTrigger> 
                
                <!--数据变化触发器 如果值等于123的时候触发事件-->
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self},Path=Text}  " Value="123">
                    <Setter Property="Foreground" Value="Blue"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <!--表示这个样式继承于BaseStyle-->
        <Style x:Key="style1" TargetType="TextBox" BasedOn="{StaticResource ResourceKey=BaseStyle}" >
        </Style>
    </Window.Resources>
    <Grid>
        <TextBox Style="{StaticResource ResourceKey=style1}"  />
        <TextBox Style="{StaticResource ResourceKey=style1}" Margin="0,75,0,0"  />
        <TextBox Style="{StaticResource ResourceKey=style1}" Margin="0,35,0,0"    /> 
    </Grid>
</Window>

 

 

 

 

 

 

v

这篇关于wpf之触发器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!