WPF中触发器的学习

WPF中触发器的学习

WPF中的触发器可以用来设置控件的状态变化的时候发生的一些事情。
本案例说明的时先定义一个资源,类型是按钮类型,添加一个出发的事件是鼠标悬停时字体大小变成30,字体变成蓝色

<Window x:Class="notes00.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:notes00"
        mc:Ignorable="d"
        xmlns:control="clr-namespace:MyControl;assembly=MyControl"
        Title="MainWindow" Height="400" Width="400"  WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <Style x:Key="ButtonTriger" TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True"><!--定义触发的事件-->
                    <Setter Property="Foreground" Value="Blue"/><!--字体变成蓝色-->
                    <Setter Property="FontSize" Value="30"/><!--字体大小变成30-->
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid >
        <Button Content="Hello" Style="{StaticResource ButtonTriger}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Window>


学习参考

发布了65 篇原创文章 · 获赞 8 · 访问量 3215

猜你喜欢

转载自blog.csdn.net/yasenRK/article/details/104170215
今日推荐