wpf之样式属性、事件、触发器

wpf的样式可以把属性一样的设置写在样式里面,这样可以减少代码量。

举个简单的例子:

<Window x:Class="WpfPictureClick.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:WpfPictureClick"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="BigFrontStyle">
            <Setter Property="Control.FontFamily" Value="Time New Roman"></Setter>
            <Setter Property="Control.FontSize" Value="18"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Style="{StaticResource BigFrontStyle}" Width="80" Height="80" Content="3333"></Button>
        <Button Grid.Row="1"  Width="80" Height="80" Content="3333"></Button>
    </Grid>
</Window>

样式需要在Window.Resources里面定义,然后在下面使用,直接上结果的图片

明显字体是不一样的,这个时候,样式的作用就体现出来了

*****************************除了设置属性之后,样式还能够设置事件*************

<Window x:Class="WpfPictureClick.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:WpfPictureClick"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="EventStyle">
            <EventSetter Event="Button.MouseEnter" Handler="FrameworkElement_MouseEnter"></EventSetter>
            <EventSetter Event="Button.MouseLeave" Handler="FrameworkElement_MouseLeave"></EventSetter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Style="{StaticResource EventStyle}" Width="80" Height="80" Content="3333"></Button>
        <Button Grid.Row="1"  Width="80" Height="80" Content="3333"></Button>
    </Grid>
</Window>

通过EventSetter来设置

处理函数如下:

 public void FrameworkElement_MouseEnter(object sender, MouseEventArgs e)
        {
            ((Button)sender).Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x2c, 0x2c, 0x2c));
        }

        private void FrameworkElement_MouseLeave(object sender, MouseEventArgs e)
        {
            ((Button)sender).Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x12, 0x57, 0x90));
        }

结果如下:

 *****************************除了设置事件之后,样式有个王牌,叫触发器*************

<Window x:Class="WpfPictureClick.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:WpfPictureClick"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="EventStyle">
            <EventSetter Event="Button.MouseEnter" Handler="FrameworkElement_MouseEnter"></EventSetter>
            <EventSetter Event="Button.MouseLeave" Handler="FrameworkElement_MouseLeave"></EventSetter>
        </Style>
        <Style x:Key="TriggerStyle" >
            <Style.Triggers>
                <Trigger Property="Button.IsMouseOver" Value="True">
                    <Setter Property="Button.Background" Value="red"></Setter>
                </Trigger>
                <Trigger Property="Button.IsMouseOver" Value="false">
                    <Setter Property="Button.Background" Value="Black"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Focusable="False" Style="{StaticResource TriggerStyle}" Width="80" Height="80" Content="3333"></Button>
        <Button Grid.Row="1" Width="80" Height="80" Content="3333"></Button>
    </Grid>
</Window>

通过Style.Triggers来设置触发器,效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值