2013-06-18 107 views
1

按下按鈕時,我不想要動畫,但只有前景和背景被改變,就像地鐵一樣。我很困惑。在WPF中:如何在按下按鈕後禁用動畫?

編輯:

創建在Expression Blend樣本4 有3種狀態:正常,IsMouseOver,IsPressed: enter image description here

編輯2:

我只是修改標題「在WPF中:如何在按下按鈕時禁用動畫?」到「在WPF中:如何在按下按鈕後禁用動畫?」

我發現一些問題:

  1. 當鼠標滑過或按下按鈕,按鈕的研究背景沒有改變;
  2. 按下按鈕後運行的動畫,我只想禁用動畫。

編輯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> 

+0

你有沒有考慮改變[風格](http://msdn.microsoft。 com/en-us/library/ms753328.aspx)的按鈕? – Clemens

+0

你嘗試了什麼?請分享您的XAML。 –

+0

@Clemens是的,我在努力。 – SubmarineX

回答

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>