2011-02-07 101 views
4

我有的ToggleButtons數量的自定義控制:如何動畫IsChecked屬性?

<Style TargetType="{x:Type local:ISA88Buttons}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
     <ControlTemplate TargetType="{x:Type local:ISA88Buttons}"> 
      <Border x:Name="PART_Border" 
        Background="{TemplateBinding Background}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}"> 
      <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup x:Name="ISA88States"> 
... 
       <VisualState x:Name="AbortingState" Storyboard="{StaticResource sbAbortingState}" /> 
       <VisualState x:Name="AbortedState" Storyboard="{StaticResource sbAbortedState}" /> 
... 
       </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 

<StackPanel Margin="1" 
         HorizontalAlignment="Center" 
         Orientation="Horizontal"> 
... 
<ToggleButton x:Name="PART_Button_Abort" 
          Margin="1" 
          IsChecked="False" 
          IsThreeState="True" 
          Tag="Abort"> 
       <ToggleButton.Style> 
        <Style TargetType="{x:Type ToggleButton}"> 
        <Setter Property="OverridesDefaultStyle" Value="True" /> 
        <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
        <Setter Property="Template"> 
         <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ToggleButton}"> 
          <Border x:Name="border" 
            Width="{TemplateBinding Width}" 
            Height="{TemplateBinding Height}" 
            Background="{StaticResource NormalAbortButtonDrawingBrush}" /> 
          <ControlTemplate.Triggers> 
          <Trigger Property="IsEnabled" Value="False"> 
           <Setter TargetName="border" Property="Background" Value="{StaticResource DisabledAbortButtonDrawingBrush}" /> 
          </Trigger> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter TargetName="border" Property="Background" Value="{StaticResource MouseOverAbortButtonDrawingBrush}" /> 
          </Trigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
           <Condition Property="IsEnabled" Value="True" /> 
           <Condition Property="IsChecked" Value="True" /> 
           </MultiTrigger.Conditions> 
           <MultiTrigger.Setters> 
           <Setter TargetName="border" Property="Background" Value="{StaticResource PressedAbortButtonDrawingBrush}" /> 
           </MultiTrigger.Setters> 
          </MultiTrigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
           <Condition Property="IsEnabled" Value="True" /> 
           <Condition Property="IsChecked" Value="{x:Null}" /> 
           </MultiTrigger.Conditions> 
           <MultiTrigger.EnterActions> 
           <BeginStoryboard x:Name="sb"> 
            <Storyboard RepeatBehavior="Forever"> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{StaticResource PressedAbortButtonDrawingBrush}" /> 
             <DiscreteObjectKeyFrame KeyTime="00:00:0.25" Value="{StaticResource NormalAbortButtonDrawingBrush}" /> 
             <DiscreteObjectKeyFrame KeyTime="00:00:0.5" Value="{StaticResource PressedAbortButtonDrawingBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </BeginStoryboard> 
           </MultiTrigger.EnterActions> 
          </MultiTrigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
        </Style> 
       </ToggleButton.Style> 
       </ToggleButton> 
... 
</StackPanel> 
      </Border> 
</ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    </Style> 

和故事:

<Storyboard x:Key="sbAbortingState">  
     <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Button_Abort" Storyboard.TargetProperty="(ToggleButton.IsChecked)"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Null}" /> 
      </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 

    <Storyboard x:Key="sbAbortedState"> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Button_Abort" Storyboard.TargetProperty="(ToggleButton.IsChecked)"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="True" /> 
      </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 

當我在中止狀態控制,按鈕中止閃爍,但是當我改變狀態,以中止錯誤的是提出:

無法在 「動畫 '器isChecked' 財產System.Windows.Controls.Primitives.Togg leButton」 使用 'System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames'

我怎樣才能動畫值之間的故事板使用這個屬性:true, false and {x:Null},不僅true and false

回答

8

的問題是,你真的不應該被設定使用ObjectAnimationUsingKeyFrames動畫一個布爾值,而應該是一個BooleanAnimationUsingKeyFrames動畫與DiscreteBooleanKeyFrame關鍵幀。

所以你的標記是:

<BooleanAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="(ToggleButton.IsChecked)"> 
     <DiscreteBooleanKeyFrame KeyTime="0" Value="{x:Null}" /> 
</BooleanAnimationUsingKeyFrames> 

<BooleanAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="(ToggleButton.IsChecked)"> 
     <DiscreteBooleanKeyFrame KeyTime="0" Value="True" /> 
</BooleanAnimationUsingKeyFrames> 

<BooleanAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="(ToggleButton.IsChecked)"> 
     <DiscreteBooleanKeyFrame KeyTime="0" Value="False" /> 
</BooleanAnimationUsingKeyFrames> 
+0

爲什麼我們需要這個``?原因BOOL默認情況下有錯誤的值 – demo 2015-11-24 16:13:41

5

我找到了解決方案。如果你想設置布爾?財產XAML,你應該這樣做:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="(ToggleButton.IsChecked)"> 
     <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Null}" /> 
</ObjectAnimationUsingKeyFrames> 

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="(ToggleButton.IsChecked)"> 
     <DiscreteObjectKeyFrame KeyTime="0"> 
     <DiscreteObjectKeyFrame.Value> 
      <sys:Boolean>True</sys:Boolean> 
     </DiscreteObjectKeyFrame.Value> 
     </DiscreteObjectKeyFrame> 
</ObjectAnimationUsingKeyFrames> 

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="(ToggleButton.IsChecked)"> 
     <DiscreteObjectKeyFrame KeyTime="0"> 
     <DiscreteObjectKeyFrame.Value> 
      <sys:Boolean>False</sys:Boolean> 
     </DiscreteObjectKeyFrame.Value> 
     </DiscreteObjectKeyFrame> 
</ObjectAnimationUsingKeyFrames> 
+0

見與BooleanAnimationUsingKeyFrames答案,這是正確的做法。 – 2014-01-23 14:03:39