如何动画IsChecked属性?

时间:2011-02-07 04:21:18

标签: c# .net wpf visual-studio-2010 .net-4.0

我有ToggleButtons数量的自定义控件:

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

当我的控制处于中止状态时,按钮Abort闪烁,但当我将状态更改为Aborted时,错误会加剧:

  

无法为'IsChecked'制作动画   在...上的财产   'System.Windows.Controls.Primitives.ToggleButton'   用一个   'System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames'

如何在三个true, false and {x:Null}之间使用Storyboard为此属性设置动画,而不仅仅是true and false

2 个答案:

答案 0 :(得分:11)

问题是您不应该使用ObjectAnimationUsingKeyFrames动画设置布尔值,而应该是具有DiscreteBooleanKeyFrame关键帧的BooleanAnimationUsingKeyFrames动画。

所以你的标记是:

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

答案 1 :(得分:5)

我找到了解决方案。如果你想设置bool? 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>