简体   繁体   English

WPF XAML 在鼠标悬停时动画/闪烁按钮

[英]WPF XAML Animate/blinking a button on mouse over

I have a custom message box with two button 'Yes' and 'No'.我有一个带有两个按钮“是”和“否”的自定义消息框。 Button 'Yes' is green and button 'No' is red. “是”按钮为绿色,“否”按钮为红色。

I apply the same style for the two buttons through a xaml file defined separately as below:我通过一个单独定义的 xaml 文件为两个按钮应用相同的样式,如下所示:

MsgBoxButtonStyle.xaml : MsgBoxButtonStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button" 

x:Key="MsgBoxButtonStyle">
        <Setter Property="Background" 

        Value="Transparent" />
        <Setter Property="TextBlock.TextAlignment" 

        Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border  Name="Border" CornerRadius="0"  

                    BorderBrush="#000" BorderThickness="1,1,1,1" 

                    Background="{TemplateBinding Background}">
                        <ContentPresenter x:Name="contentPresenter" 

                        ContentTemplate="{TemplateBinding ContentTemplate}" 

                        Content="{TemplateBinding Content}" 

                        HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 

                        Margin="{TemplateBinding Padding}" 

                        VerticalAlignment="{TemplateBinding VerticalAlignment}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

These two buttons are place in my WPF window as below:这两个按钮位于我的 WPF 窗口中,如下所示:

<Window x:Class="My.XAML.Controls.Windows.WpfMessageBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="/My.XAML;component/Styles/MsgBoxButtonStyle.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources> 

   <Button Name="btnYes" Content="Yes"                             
            Click="Button_Click" Foreground="Black" 
            Style="{StaticResource MsgBoxButtonStyle}"    
            Background="#b6dbd6" 
            VerticalAlignment="Center" HorizontalAlignment="Stretch"  
            VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>

    <Button Name="btnNo" Content="No"                             
            Click="Button_Click" Foreground="Black" 
            Style="{StaticResource MsgBoxButtonStyle}"
            Background="#dbb6b6" 
            VerticalAlignment="Center" HorizontalAlignment="Stretch"  
            VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
</Window>

Now, I would like to perform a nice effect when mouse is positioned over the buttons, some kind of blinking or whatever using storyboard and dependant on the color of the button.现在,当鼠标位于按钮上时,我想执行一个很好的效果,某种闪烁或使用故事板并依赖于按钮的颜色的任何东西。

Also I would like to put this storyboard within my existing style file MsgBoxButtonStyle.xaml and no put each storyboard in the window for each button, i want to share it.另外我想把这个故事板放在我现有的样式文件 MsgBoxButtonStyle.xaml 中,而不是把每个故事板放在每个按钮的窗口中,我想分享它。

How can I do this?我怎样才能做到这一点? some one can provide me a nice effect for buttons?有人可以为我提供一个很好的按钮效果吗?

Just add triggers to the style.只需在样式中添加触发器即可。 Here's an example to start you off.这是一个让你开始的例子。

 <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ThicknessAnimation Duration="0:0:0.250" To="0" 
                                                            Storyboard.TargetProperty="BorderThickness" />
                            <DoubleAnimation Duration="0:0:0.550" To="120" 
                                                            Storyboard.TargetProperty="Height" />
                            <DoubleAnimation Duration="0:0:0.550" To="120" 
                                                            Storyboard.TargetProperty="Width" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ThicknessAnimation Duration="0:0:0.250" To="0" 
                                                            Storyboard.TargetProperty="BorderThickness" />
                            <DoubleAnimation Duration="0:0:0.550" To="100" 
                                                            Storyboard.TargetProperty="Height" />
                            <DoubleAnimation Duration="0:0:0.550" To="100" 
                                                            Storyboard.TargetProperty="Width" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM