2011-03-10 100 views
3

我剛剛開始尋找WPF,並且遇到了有趣的問題。目的是讓一個按鈕具有簡單的形式,當用戶將鼠標移動到按鈕上時,按鈕應該移開,因此可以說整個過程如下所示: 1.位置A處的按鈕。 2.將鼠標懸停在按鈕它移動到位置B. 3.將鼠標移到按鈕上,它將移動到位置C. 4.將鼠標移到按鈕上,它將移回到位置A. 5.將鼠標移到按鈕上,它會再次移動到位置B.WPF動畫問題

目標是用戶永遠不能點擊按鈕,該按鈕在A到B之間運行,C到A到B,它應該繼續運行,直到用戶放棄爲止。 我想通過使用觸發器和動畫來實現這一點;問題在於觸發器僅工作一次,在按鈕返回到位置之後觸發器不會再次觸發,因此按鈕不再移動。 以下是來自xaml文件的代碼。

<Window x:Class="Test_1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
    <Style TargetType="Button"> 
     <Style.Triggers>     
      <MultiTrigger x:Name="Aaaa" > 
       <MultiTrigger.Conditions> 
        <Condition Property="Margin" Value="51,58,0,0"/> 
        <Condition Property="IsMouseOver" Value="True"/> 
       </MultiTrigger.Conditions> 
       <MultiTrigger.EnterActions> 
        <BeginStoryboard Name="SB2" > 
         <Storyboard Duration="0:0:1"> 
          <ThicknessAnimation Storyboard.TargetProperty="Margin" To="249,199,0,0" /> 
         </Storyboard> 
        </BeginStoryboard>       
       </MultiTrigger.EnterActions> 
      </MultiTrigger> 
      <MultiTrigger x:Name="Bbbbb"> 
       <MultiTrigger.Conditions> 
        <Condition Property="Margin" Value="249,199,0,0"/> 
        <Condition Property="IsMouseOver" Value="True"/> 
       </MultiTrigger.Conditions> 
       <MultiTrigger.EnterActions> 
        <BeginStoryboard Name="SB3"> 
         <Storyboard Duration="0:0:1"> 
          <ThicknessAnimation Storyboard.TargetProperty="Margin" To="301,66,0,0" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </MultiTrigger.EnterActions> 
      </MultiTrigger> 
      <MultiTrigger x:Name="Ccccc"> 
       <MultiTrigger.Conditions> 
        <Condition Property="Margin" Value="301,66,0,0"/> 
        <Condition Property="IsMouseOver" Value="True"/> 
       </MultiTrigger.Conditions> 
       <MultiTrigger.EnterActions> 
        <BeginStoryboard Name="SB4"> 
         <Storyboard Duration="0:0:1"> 
          <ThicknessAnimation Storyboard.TargetProperty="Margin" To="51,58,0,0"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </MultiTrigger.EnterActions> 
      </MultiTrigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="51,58,0,0" Name="button1" VerticalAlignment="Top" Width="75" />     
</Grid> 

謝謝!!

回答

1

我相信你需要停止你的故事板才能再次使用它。因此,這裏是有點爲你的榜樣,以得到它的工作黑客攻擊(在各個分鏡圖的退出動作停止先前執行的故事板):

<Style TargetType="Button"> 
     <Style.Triggers> 
      <MultiTrigger x:Name="Aaaa"> 
       <MultiTrigger.Conditions> 
        <Condition Property="Margin" Value="51,58,0,0"/> 
        <Condition Property="IsMouseOver" Value="True"/> 
       </MultiTrigger.Conditions> 
       <MultiTrigger.EnterActions > 
        <BeginStoryboard Name="SB2" > 
         <Storyboard Duration="0:0:1" > 
          <ThicknessAnimation Storyboard.TargetProperty="Margin" To="249,199,0,0" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </MultiTrigger.EnterActions> 
       <MultiTrigger.ExitActions> 
        <StopStoryboard BeginStoryboardName="SB4" /> 
       </MultiTrigger.ExitActions> 
      </MultiTrigger> 
      <MultiTrigger x:Name="Bbbbb"> 
       <MultiTrigger.Conditions> 
        <Condition Property="Margin" Value="249,199,0,0"/> 
        <Condition Property="IsMouseOver" Value="True"/> 
       </MultiTrigger.Conditions> 
       <MultiTrigger.EnterActions> 
        <BeginStoryboard Name="SB3"> 
         <Storyboard Duration="0:0:1"> 
          <ThicknessAnimation Storyboard.TargetProperty="Margin" To="301,66,0,0" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </MultiTrigger.EnterActions> 
       <MultiTrigger.ExitActions> 
        <StopStoryboard BeginStoryboardName="SB2" /> 
       </MultiTrigger.ExitActions> 
      </MultiTrigger> 
      <MultiTrigger x:Name="Ccccc"> 
       <MultiTrigger.Conditions> 
        <Condition Property="Margin" Value="301,66,0,0"/> 
        <Condition Property="IsMouseOver" Value="True"/> 
       </MultiTrigger.Conditions> 
       <MultiTrigger.EnterActions> 
        <BeginStoryboard Name="SB4"> 
         <Storyboard Duration="0:0:1"> 
          <ThicknessAnimation Storyboard.TargetProperty="Margin" To="51,58,0,0"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </MultiTrigger.EnterActions> 
       <MultiTrigger.ExitActions> 
        <StopStoryboard BeginStoryboardName="SB3" /> 
       </MultiTrigger.ExitActions> 
      </MultiTrigger> 
     </Style.Triggers> 
    </Style> 
1

好像你的故事板,一旦他們完成了不會停止動畫。嘗試在開始新的故事板之前停止其他故事板StopStoryboard

<Style TargetType="Button"> 
    <Style.Triggers> 
     <MultiTrigger x:Name="Aaaa" > 
      <MultiTrigger.Conditions> 
       <Condition Property="Margin" Value="51,58,0,0"/> 
       <Condition Property="IsMouseOver" Value="True"/> 
      </MultiTrigger.Conditions> 
      <MultiTrigger.EnterActions> 
       <StopStoryboard BeginStoryboardName="SB3"/> 
       <StopStoryboard BeginStoryboardName="SB4"/> 
       <BeginStoryboard Name="SB2"> 
        <Storyboard Duration="0:0:1"> 
         <ThicknessAnimation Storyboard.TargetProperty="Margin" To="249,199,0,0" /> 
        </Storyboard> 
       </BeginStoryboard> 
      </MultiTrigger.EnterActions> 
     </MultiTrigger> 
     <MultiTrigger x:Name="Bbbbb"> 
      <MultiTrigger.Conditions> 
       <Condition Property="Margin" Value="249,199,0,0"/> 
       <Condition Property="IsMouseOver" Value="True"/> 
      </MultiTrigger.Conditions> 
      <MultiTrigger.EnterActions> 
       <StopStoryboard BeginStoryboardName="SB2"/> 
       <StopStoryboard BeginStoryboardName="SB4"/> 
       <BeginStoryboard Name="SB3"> 
        <Storyboard Duration="0:0:1"> 
         <ThicknessAnimation Storyboard.TargetProperty="Margin" To="301,66,0,0" /> 
        </Storyboard> 
       </BeginStoryboard> 
      </MultiTrigger.EnterActions> 
     </MultiTrigger> 
     <MultiTrigger x:Name="Ccccc"> 
      <MultiTrigger.Conditions> 
       <Condition Property="Margin" Value="301,66,0,0"/> 
       <Condition Property="IsMouseOver" Value="True"/> 
      </MultiTrigger.Conditions> 
      <MultiTrigger.EnterActions> 
       <StopStoryboard BeginStoryboardName="SB2"/> 
       <StopStoryboard BeginStoryboardName="SB3"/> 
       <BeginStoryboard Name="SB4"> 
        <Storyboard Duration="0:0:1"> 
         <ThicknessAnimation Storyboard.TargetProperty="Margin" To="51,58,0,0" /> 
        </Storyboard> 
       </BeginStoryboard> 
      </MultiTrigger.EnterActions> 
     </MultiTrigger> 
    </Style.Triggers> 
</Style>