WPF之触发器

WPF之触发器

事件触发器:通过事件改变样式
数据触发器:通过数据改变样式

样式定义流程

1. Style
2. 命名 x:key
3. 指定目标控件 TargetType
4. 确定触发器:Trigger 
5. setter 设置我们想要样式

一 事件触发器


    <Window.Resources>
        <Style x:Key="btn1Style" TargetType="{x:Type Button}">
            <Style.Triggers>
                <!-- 鼠标悬停时 -->

                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
                <!-- 按钮按下时 -->

                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Foreground" Value="Green"/>
                </Trigger>
                <!-- 按钮禁用时 -->

                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="Blue"/>
                </Trigger>

            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <StackPanel Orientation="Vertical" Margin="10">
            <Button Content="Button A" Style="{StaticResource btn1Style}"></Button>
            <Button Content="Button A" IsEnabled="False" Style="{StaticResource btn1Style}"></Button>

        </StackPanel>

    </Grid>


二 数据触发器(DataTrigger)

通过数据绑定触发样式变化:

<Window x:Class="WPF之触发器.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:WPF之触发器"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="btn1Style" TargetType="{x:Type Button}">
            <Style.Triggers>
                <!-- 鼠标悬停时 -->

                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
                <!-- 按钮按下时 -->

                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Foreground" Value="Green"/>
                </Trigger>
                <!-- 按钮禁用时 -->

                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="Blue"/>
                </Trigger>

            </Style.Triggers>
        </Style>


        <Style x:Key="tbxStyle1" TargetType="TextBlock">
            <Setter Property="Foreground" Value="Black"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsImportant}" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <Grid>
        <StackPanel Orientation="Vertical" Margin="10">
            <Button Content="Button A" Style="{StaticResource btn1Style}"></Button>
            <Button Content="Button A" IsEnabled="False" Style="{StaticResource btn1Style}"></Button>

            <!--二 数据触发器(DataTrigger)-->
            <TextBlock Text="xxx" Style="{StaticResource tbxStyle1}"></TextBlock>
            <Button Click="Button_Click" Width="30" Height="30"> </Button>
        </StackPanel>

    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF之触发器
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public Test test = new Test();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = test;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            test.IsImportant = true;
        }
    }

    public class Test : INotifyPropertyChanged
    {

        private bool id;
        // 属性方法
        public bool IsImportant
        {
            // 获取
            get => id;
            // 设置值
            set
            {
                id = value;
                // 触发调用事件(委托)变量
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsImportant"));
            }
        }
        // 属性改变事件
        public event PropertyChangedEventHandler PropertyChanged;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值