WPF-样式

<Window x:Class="Wpf.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window2" Height="300" Width="300">
    <Window.Resources>
        <FontFamily x:Key="a1">Times New Roman</FontFamily>
        <sys:Double x:Key="a2">18</sys:Double>
        <FontWeight x:Key="a3">Bold</FontWeight>
        
        <Style x:Key="Big">
<Style.Triggers>
                <Trigger Property="Control.IsFocused" Value="true">
                    <Setter  Property="Control.Background" >
                        <Setter.Value>
                            <LinearGradientBrush>
                                <GradientStop Color="Chocolate" Offset="0.3"></GradientStop>
                                <GradientStop Color="White"></GradientStop>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>

            <Setter Property="Control.FontSize" Value="35"></Setter>
            <Setter Property="Control.Background" >
                <Setter.Value>
                    <LinearGradientBrush  StartPoint="0,0" EndPoint="1,1" >
                        <GradientStop Color="Beige" Offset="0.5"></GradientStop>
                        <GradientStop Color="HotPink"></GradientStop>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox FontFamily="{StaticResource ResourceKey=a1}" FontWeight="{StaticResource ResourceKey=a3}" FontSize="{StaticResource ResourceKey=a2}"></TextBox>
            <TextBox  Style="{StaticResource ResourceKey=Big}"></TextBox>
            <TextBox x:Name="txt1"></TextBox>
        </StackPanel>
    </Grid>
</Window>

也可在后台代码输入—

txt1.Style = (Style)this.FindResource("Big");

以上为简单的样式行为,样式可分为:关联事件样式-多层样式-通过类型自动应用样式-触发器样式

简单动画:实现文本框的文字放大缩小

<Window x:Class="Wpf.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" Height="300" Width="300">
    <Window.Resources>
        <Style  x:Key="big" TargetType="TextBox">
            <Style.Triggers>
                <EventTrigger RoutedEvent="TextBox.MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Duration="0:0:0.3" Storyboard.TargetProperty="FontSize" To="50"></DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="TextBox.MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="FontSize" ></DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

        <StackPanel>
        <TextBox   Style="{StaticResource big}" Height="100">3333312222222222222222222222222222222222222222222222222222221</TextBox>
    </StackPanel>

</Window>
三思而又行。
原文地址:https://www.cnblogs.com/jun-jie/p/3728071.html