zoukankan      html  css  js  c++  java
  • WPF之触发器

    简单触发器
    <
    Window x:Class="WpfApp.Window1" 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:WpfApp" mc:Ignorable="d" Title="Window1" Height="450" Width="800"> <Window.Resources> <Style x:Key="BigFontButton"><!--命名为大字体--> <Setter Property="Control.FontFamily" Value=" Times new Roman"></Setter> <Setter Property="Control.FontSize" Value="32"></Setter> <Style.Triggers><!--触发器集合--> <Trigger Property="Control.IsFocused" Value="True"><!--获取焦点触发--> <Setter Property="Control.Foreground" Value="DarkRed"></Setter><!--触发内容为变红色--> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Button Content="Button" Style="{StaticResource BigFontButton}"/> </Grid> </Window>

    结果button获取焦点后 字体变红

    <Window x:Class="WpfApp.Window1"
            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:WpfApp"
            mc:Ignorable="d"
            Title="Window1" Height="450" Width="800">
        <Window.Resources>
            <Style x:Key="BigFontButton"><!--命名为大字体-->
                <Setter Property="Control.FontFamily" Value=" Times new Roman"></Setter>
                <Setter Property="Control.FontSize" Value="32"></Setter>
                <Style.Triggers><!--触发器集合-->
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Control.IsFocused" Value="True"></Condition>
                            <Condition Property="Control.IsMouseOver" Value="True"></Condition>
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter Property="Control.Foreground" Value="Red"></Setter>
                        </MultiTrigger.Setters>
                    </MultiTrigger>
                    
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <StackPanel VerticalAlignment="Center">
            <Button Content="Button" Style="{StaticResource BigFontButton}"/>
            <TextBox Margin="10" Text="必须满足两个条件才能触发触发器"/>
        </StackPanel>
    </Window>

    事件触发器

    <Window x:Class="WpfApp.Window2"
            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:WpfApp"
            mc:Ignorable="d"
            Title="Window2" Height="450" Width="800">
        <Window.Resources>
            <Style x:Key="EventTrriger">
                <Style.Setters>
                    <Setter Property="Control.FontSize" Value="red"></Setter>
                    <Setter Property="Control.FontFamily" Value="Yellow"></Setter>
                </Style.Setters>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Mouse.MouseEnter">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize" To="48"></DoubleAnimation>
                                </Storyboard>
                             </BeginStoryboard>
                        </EventTrigger.Actions> 
                    </EventTrigger><!--路由事件-->
                    
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <StackPanel>
            <Button Name="bt_1" Content="事件触发器" Style="{StaticResource EventTrriger}"/>
        </StackPanel>
    </Window>
  • 相关阅读:
    webpack打包提示: Uncaught Error: Cannot find module 'strip-ansi'
    CentOS 7.6 内网穿透服务lanproxy部署
    《这是全网最硬核redis总结,谁赞成,谁反对?》六万字大合集
    网络监控解决方案及拓扑图
    漫画:什么是 “混合云”?
    听说过Paas、Saas和Iaas,那你听说过Apaas吗?
    Squid设置用户名密码
    别再售卖 5块钱 的 Win10 激活码了,后果很严重
    Jackson 实体转Json、Json转实体
    Spring
  • 原文地址:https://www.cnblogs.com/javier520/p/10879150.html
Copyright © 2011-2022 走看看