在WPF中:如何在按下按钮后禁用动画?

时间:2013-06-18 06:59:17

标签: wpf templates animation button styles

按下按钮时我不需要动画,但只改变前景和背景,就像地铁风格一样。我很困惑。

修改

我在Expression Blend 4中创建了一个示例。 有3种状态:Normal,IsMouseOver,IsPressed: enter image description here

编辑2:

我只修改标题“在WPF中:如何在按下按钮时禁用动画?” to“在WPF中:如何在按下按钮后禁用动画?”

我发现了一些问题:

  1. 当鼠标结束或按下按钮时,按钮的背景不会改变;
  2. 按下按钮后动画运行,我只想禁用动画。
  3. 编辑3:

    此.xaml可以在VS中运行。

    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication17.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">
    <Window.Resources>
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="BorderBrush" Value="Black"/>
                    <Setter Property="Background" Value="Black"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="Black"/>
                    <Setter Property="BorderBrush" Value="Black"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="White"/>
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
        </Style>
    
    </Window.Resources>
    
    <Grid x:Name="LayoutRoot" >
        <Button Content="Button" 
                HorizontalAlignment="Left" 
                VerticalAlignment="Top" 
                Width="75" 
                Style="{DynamicResource ButtonStyle1}"/>
    </Grid>
    

1 个答案:

答案 0 :(得分:1)

尝试此操作(创建一个新项目并将其粘贴到您的定义下方):

<Window.Resources>
        <Style x:Key="NoAnimations" TargetType="{x:Type Button}" >
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="Background" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border BorderBrush="Black" BorderThickness="1">
                            <Border Name="border" Background="{TemplateBinding Background}" Padding="3">
                                <Grid>
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content" />
                                </Grid>
                            </Border>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                               <Setter Property="Background" Value="Black"></Setter>
                                <Setter Property="Foreground" Value="White"></Setter>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="False">
                                <Setter Property="Background" Value="White"></Setter>
                                <Setter Property="Foreground" Value="Black"></Setter>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" Value="Black"></Setter>
                                <Setter Property="Foreground" Value="Red"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <Button Style="{StaticResource NoAnimations}"
                Content="Testing"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Margin="20"

                />
    </StackPanel>