为什么 XAML 定义的动画会导致 System.InvalidOperationException

Why XAML defined animation causes System.InvalidOperationException

我需要在 XAML 中定义简单的动画(没有代码隐藏),必须通过更改有界布尔值 属性 来旋转按钮的背景图像。我在 XAML:

中有一个按钮
<Button Style="{StaticResource btnStyle}" />

在 Window 的 Resources 部分,我创建了以下内容:

<RotateTransform Angle="180" x:Key="rotAt180" />

<Style TargetType="Button" x:Key="btnStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image Source="Images\pic.png" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding MyBoolProp}" Value="False">
            <Setter Property="RenderTransform" Value="{StaticResource rotAt180}" />
            <!-- This animation works good -->
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation From="0" To="180" Duration="0:0:0.2" Storyboard.TargetProperty="RenderTransform.Angle" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <!-- This animation causes exception -->
            <DataTrigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation From="180" To="0" Duration="0:0:0.2" Storyboard.TargetProperty="RenderTransform.Angle" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.ExitActions> 
        </DataTrigger>
    </Style.Triggers>
</Style>

MyBoolProp 由 True 初始化。 第一个动画是完美的作品。但是第二个动画导致 System.InvalidOperationException in PresentationFramework.dll ("Unable to resolve all property references in the "RenderTransform.Angle" 属性 路径。检查相应的对象是否支持此类属性" =29=]).

我尝试将第二个动画移动到触发器中,其中 MyBoolProp 为 True:

 <DataTrigger Binding="{Binding MyBoolProp}" Value="True">
 <DataTrigger.EnterActions>
     <BeginStoryboard>
         <Storyboard>
             <DoubleAnimation From="180" To="0" Duration="0:0:0.2" Storyboard.TargetProperty="RenderTransform.Angle" />
          </Storyboard>
      </BeginStoryboard>
  </DataTrigger.EnterActions> 

但结果是一样的。这样的认识有什么问题呢?

RenderTransform 必须在 Style 中设置,而不是在 DataTrigger 中设置:

<Style TargetType="Button" x:Key="btnStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image Source="Images\pic.png" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="RenderTransform" Value="{StaticResource rotAt180}"/>
    <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />

    <Style.Triggers>
        <DataTrigger Binding="{Binding MyBoolProp}" Value="False">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation To="0" Duration="0:0:0.2"
                            Storyboard.TargetProperty="RenderTransform.Angle" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation To="180" Duration="0:0:0.2"
                            Storyboard.TargetProperty="RenderTransform.Angle" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>