我想使用图像来显示状态,我希望其中一个图像成为加载微调器。我正在使用触发器旋转图像时旋转图像。但是,这种方法似乎在不可变对象实例上抛出错误“无法动画”(RenderTransform)。(RotateTransform.Angle)'
<ItemsControl ItemsSource="{Binding StatefulViewModels}"
HorizontalContentAlignment="Stretch">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Expander IsEnabled="{Binding IsCompleted}">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<Image Margin="5" Width="18">
<Image.RenderTransform>
<RotateTransform Angle="0" CenterX="9" CenterY="9"/>
</Image.RenderTransform>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentState}" Value="{x:Static enums:State.CompletedWithErrors}">
<Setter Property="Source" Value="../Images/Failed.png" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentState}" Value="{x:Static enums:State.CompletedSuccessfully}">
<Setter Property="Source" Value="../Images/Success.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentState}" Value="{x:Static enums:State.Unexecuted}">
<Setter Property="Source" Value="../Images/Waiting.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentState}" Value="{x:Static enums:State.Running}">
<Setter Property="Source" Value="../Images/Running.png"/>
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)"
From="0"
To="360"
Duration="0:0:2"
RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)"
To="0"
Duration="0:0:0"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Margin="5" Text="{Binding Path=Title}" FontSize="16"/>
</StackPanel>
</Expander.Header>
<ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource ResultTemplateSelector}"/>
</Expander>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我读过一篇MSDN文章explaining about datatriggers and storyboards,但我想不出这种方法的一个很好的替代方案。
实现这个/修正我的方法的正确方法是什么?
答案 0 :(得分:4)
尝试使用样式设置器设置Image的RenderTransform属性,而不是使用本地值设置它:
<Image Margin="5" Width="18">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="0" CenterX="9" CenterY="9"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentState}" Value="{x:Static enums:State.CompletedWithErrors}">
<Setter Property="Source" Value="../Images/Failed.png" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentState}" Value="{x:Static enums:State.CompletedSuccessfully}">
<Setter Property="Source" Value="../Images/Success.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentState}" Value="{x:Static enums:State.Unexecuted}">
<Setter Property="Source" Value="../Images/Waiting.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentState}" Value="{x:Static enums:State.Running}">
<Setter Property="Source" Value="/Images/Running.png"/>
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)"
From="0"
To="360"
Duration="0:0:2"
RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)"
To="0"
Duration="0:0:0"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>