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>
  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/javier520/p/10879150.html
Copyright © 2011-2022 走看看