WPF 按钮样式只触发器(3)

来源:互联网 发布:矩阵的奇异值和特征值 编辑:程序博客网 时间:2024/05/18 03:27

今天下班之前在更新一个  就是让我们都头疼的触发器 我对触发器现在也只是知道一点 很多新的功能 研究到后 我会更新 如果懂的人 可以回复我 告诉我一些新的触发器写法 这样我很非常开心 好了 那么今天我就给大家讲一个 最简单的触发器 写法

首先还是在APP里面定义 按钮的样式  按钮的样式 追加触发器 

  <Style x:Key="buttonTemplate" TargetType="Button" >            <!--修改模板属性-->            <Setter Property="Template">                <Setter.Value>                    <!--控件模板-->                    <ControlTemplate TargetType="Button">                        <!--只有Grid才能装下这么多Child-->                        <Grid>                            <!--带特效的底层背景-->                            <Border x:Name="back" Opacity="0.8" CornerRadius="3">                                <Border.BitmapEffect>                                    <OuterGlowBitmapEffect Opacity="0.7" GlowSize="0" GlowColor="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" />                                </Border.BitmapEffect>                                <Border.Background>                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1.5">                                        <GradientBrush.GradientStops>                                            <GradientStopCollection>                                                <GradientStop Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" Offset="0"/>                                                <GradientStop Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" Offset="0.4"/>                                                <GradientStop Color="#FFF" Offset="1"/>                                            </GradientStopCollection>                                        </GradientBrush.GradientStops>                                    </LinearGradientBrush>                                </Border.Background>                                <Border x:Name="fore" BorderThickness="1" CornerRadius="3" BorderBrush="#5555">                                    <Border.Background>                                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">                                            <GradientBrush.GradientStops>                                                <GradientStopCollection>                                                    <GradientStop Color="#6FFF" Offset="0.5"/>                                                    <GradientStop Color="#1111" Offset="0.51"/>                                                </GradientStopCollection>                                            </GradientBrush.GradientStops>                                        </LinearGradientBrush>                                    </Border.Background>                                    <ContentPresenter x:Name="Content"  HorizontalAlignment="Center" VerticalAlignment="Center">                                        <ContentPresenter.BitmapEffect>                                            <DropShadowBitmapEffect Color="#000" Direction="-90" ShadowDepth="2" Softness="0.1" Opacity="0.3" />                                        </ContentPresenter.BitmapEffect>                                    </ContentPresenter>                                                           </Border>                            </Border>                        </Grid>                        <!--触发器-->                        <ControlTemplate.Triggers>                            <Trigger Property="Button.IsPressed" Value="True">                                <Setter Property="RenderTransform">                                    <Setter.Value>                                        <ScaleTransform ScaleX=".9" ScaleY=".9"/>                                    </Setter.Value>                                </Setter>                                <Setter Property="RenderTransformOrigin" Value=".5,.5"/>                            </Trigger>                            <!--鼠标移入移出-->                            <Trigger Property="IsMouseOver" Value="True">                                <Trigger.EnterActions>                                    <BeginStoryboard>                                        <Storyboard>                                            <DoubleAnimation To="6" Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />                                            <ColorAnimation To="#AFFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />                                            <ColorAnimation To="#3FFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />                                        </Storyboard>                                    </BeginStoryboard>                                </Trigger.EnterActions>                                <Trigger.ExitActions>                                    <BeginStoryboard>                                        <Storyboard>                                            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />                                            <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />                                            <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />                                        </Storyboard>                                    </BeginStoryboard>                                </Trigger.ExitActions>                            </Trigger>                            <Trigger Property="IsPressed" Value="True">                                <Trigger.EnterActions>                                    <BeginStoryboard>                                        <Storyboard>                                            <DoubleAnimation To="3" Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />                                            <ColorAnimation To="#3AAA" Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />                                            <ColorAnimation To="#2111" Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />                                        </Storyboard>                                    </BeginStoryboard>                                </Trigger.EnterActions>                                <Trigger.ExitActions>                                    <BeginStoryboard>                                        <Storyboard>                                            <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />                                            <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />                                            <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />                                        </Storyboard>                                    </BeginStoryboard>                                </Trigger.ExitActions>                            </Trigger>                                                                                                            </ControlTemplate.Triggers>                    </ControlTemplate>                </Setter.Value>            </Setter>        </Style>

触发器 触发了2个方法  鼠标移到按钮上的时候 按钮变 渐变色 鼠标点击时候 按钮缩小  大家可以试试


原创粉丝点击