2011-07-13 129 views
0

我有下面的WPF XAML試圖根據複選框的IsChecked屬性設置動畫2矩形的可見性。所以Checked意味着藍色框出現,紅色框在一秒鐘後消失,Unchecked意味着紅色框出現,藍色框消失,Undetermined表示不可見。 我正在使用故事板,但我錯過了一些東西。它工作的第一個觸發器,但一些動畫屬性卡住了。重新觸發WPF動畫

<DataTemplate x:Key="dt"> 
       <DockPanel> 
        <CheckBox x:Name="ckToggle" Content="Toggle" DockPanel.Dock="Top" IsThreeState="True"/> 
        <StackPanel Orientation="Horizontal"> 
         <Rectangle x:Name="a" Visibility="Hidden" Width="100" Height="100" Fill = "Red"/> 
         <Rectangle x:Name="b" Visibility="Hidden" Width="100" Height="100" Fill = "Blue"/> 
        </StackPanel> 
       </DockPanel> 

       <DataTemplate.Triggers> 

        <DataTrigger Binding="{Binding ElementName=ckToggle, Path=IsChecked}" Value="True"> 
         <DataTrigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="a" Storyboard.TargetProperty="Visibility"> 
             <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </BeginStoryboard> 
          <BeginStoryboard> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="b" Storyboard.TargetProperty="Visibility"> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/> 
             <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Hidden}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </BeginStoryboard> 
         </DataTrigger.EnterActions> 
         <DataTrigger.ExitActions> 
         </DataTrigger.ExitActions> 

        </DataTrigger> 

        <DataTrigger Binding="{Binding ElementName=ckToggle, Path=IsChecked}" Value="False"> 
          <DataTrigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="b" Storyboard.TargetProperty="Visibility" > 
             <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </BeginStoryboard> 
          <BeginStoryboard> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="a" Storyboard.TargetProperty="Visibility"> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/> 
              <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Hidden}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </BeginStoryboard> 
         </DataTrigger.EnterActions> 
         <DataTrigger.ExitActions> 
         </DataTrigger.ExitActions> 
        </DataTrigger> 
       </DataTemplate.Triggers> 
      </DataTemplate> 

 <ContentControl ContentTemplate="{StaticResource dt}"/> 

如果有人可以幫助,感謝 布萊恩

回答

0

的問題,你有一個wpf故事板的財產。就是這樣的情況,當你開始一個故事板時,它將永遠不會停止,直到你告訴它。所以當你打電話給第二個故事板時,你所說的第一個故事板仍在運行,並且遇到了麻煩。走出去的方式如下: 首先將每個觸發器的兩個故事板合併爲一個(您可以在一個故事板中放置多個動畫,您不必這樣做,但它使事情變得更加簡單..)。現在爲其餘兩個BeginStoryboard調用中的每一個添加一個名稱,並將相同名稱的StopStoryboard調用添加到exitActions塊中的每個dataTrigger。我結束了以下代碼(希望),如你所願:

<Window x:Class="Test.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Test" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <DataTemplate x:Key="dt"> 
     <DockPanel> 
      <CheckBox x:Name="ckToggle" Content="Toggle" DockPanel.Dock="Top" IsThreeState="True"/> 
      <StackPanel Orientation="Horizontal"> 
       <Rectangle x:Name="a" Visibility="Hidden" Width="100" Height="100" Fill = "Red"/> 
       <Rectangle x:Name="b" Visibility="Hidden" Width="100" Height="100" Fill = "Blue"/> 
      </StackPanel> 
     </DockPanel> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding ElementName=ckToggle, Path=IsChecked}" Value="True"> 
       <DataTrigger.EnterActions> 
        <BeginStoryboard Name="st1"> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="a" Storyboard.TargetProperty="Visibility"> 
           <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/> 
          </ObjectAnimationUsingKeyFrames> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="b" Storyboard.TargetProperty="Visibility"> 
           <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/> 
           <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Hidden}"/> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </BeginStoryboard> 
       </DataTrigger.EnterActions> 
       <DataTrigger.ExitActions> 
        <StopStoryboard BeginStoryboardName="st1"/> 
       </DataTrigger.ExitActions> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding ElementName=ckToggle, Path=IsChecked}" Value="False"> 
       <DataTrigger.EnterActions> 
        <BeginStoryboard Name="st2"> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="b" Storyboard.TargetProperty="Visibility"> 
           <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/> 
          </ObjectAnimationUsingKeyFrames> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="a" Storyboard.TargetProperty="Visibility"> 
           <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/> 
           <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Hidden}" /> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </BeginStoryboard> 
       </DataTrigger.EnterActions> 
       <DataTrigger.ExitActions> 
        <StopStoryboard BeginStoryboardName="st2"/> 
       </DataTrigger.ExitActions> 
      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 
</Window.Resources> 
<ContentControl ContentTemplate="{StaticResource dt}"/></Window> 
+0

嘿@selen,現貨!你是一個傳奇。謝謝,布萊恩 – bryan