0

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

<Button Style="{StaticResource btnStyle}" />

在窗口的资源部分中,我创建以下内容:

<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 初始化。第一个动画是完美的作品。但是第二个动画会导致 PresentationFramework.dll 中的 System.InvalidOperationException(“无法解析“RenderTransform.Angle”属性路径中的所有属性引用。请检查相应的对象是否支持此类属性”)。

我试图将第二个动画移动到触发器中,其中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> 

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

4

1 回答 1

1

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>
于 2018-11-08T19:27:05.170 回答