在GroupStyle.HeaderTemplate中为动画指定PropertyPath语法

时间:2014-09-02 11:19:17

标签: wpf xaml

我试图在DataGrid上的GroupStyle标头内执行旋转动画,但我无法获取Storyboard.TargetProperty属性的PropertyPath语法。这里有一个包含的示例来突出我的问题

<Window x:Class="ImageRotateTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <x:Array Type="sys:String">
        <sys:String>One</sys:String>
        <sys:String>One</sys:String>
        <sys:String>Three</sys:String>
        <sys:String>Four</sys:String>
        <sys:String>Four</sys:String>
    </x:Array>
</Window.DataContext>

<Window.Resources>
    <CollectionViewSource Source="{Binding}" x:Key="ViewSource">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription></PropertyGroupDescription>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

<DataGrid ItemsSource="{Binding Source={StaticResource ViewSource}}" AutoGenerateColumns="False">
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}">
                        <TextBlock.RenderTransform>
                            <RotateTransform/>
                        </TextBlock.RenderTransform>
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Name}" Value="Three">
                                        <DataTrigger.EnterActions>
                                            <BeginStoryboard x:Name="Test">
                                                <Storyboard>
                                                    <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle" To="360" Duration="0:0:0.800" RepeatBehavior="Forever"/>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </DataTrigger.EnterActions>
                                        <DataTrigger.ExitActions>
                                            <StopStoryboard BeginStoryboardName="Test" />
                                        </DataTrigger.ExitActions>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </DataGrid.GroupStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding}" Header="Item" />
    </DataGrid.Columns>
</DataGrid>

这里的结果应该是组头应该仅针对&#34; Three&#34;小组而不是任何其他人。但是,运行此操作会导致以下异常

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in     PresentationFramework.dll

Additional information: Cannot resolve all property references in the property path 'RenderTransform.Angle'. Verify that applicable objects support the properties.

我已经为Storyboard.TargetProperty尝试了以下内容但没有成功

(TextBlock.RenderTransform).(RotateTransform.Angle)
(RenderTransform).(Angle)
RenderTransform.Angle

我的问题是;如何使用属性路径语法引用TextBlock.RenderTransform?

2 个答案:

答案 0 :(得分:1)

您的代码几乎可以正常工作。你只需要做一个改变:

Storyboard.TargetProperty="RenderTransform.(RotateTransform.Angle)"

我仅使用TextBlock对其进行了测试,并且可以看到旋转,所以如果您无法在DataGrid中看到它旋转,您的Name属性{{1 }}不正确,或者其值不是预期的Binding

"Three"

答案 1 :(得分:-2)

对于旋转对象,这是一个简单而且最好的例子..

<Window x:Class="Animation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animated Rectangle" Height="350" Width="525">
<Grid>
    <StackPanel Margin="10">
        <Image Name="MyImage" Source="e:\a.jpg" Width="100" Margin="50" ></Image>
        <Rectangle
            Name="MyRectangle"
            Width="100" 
            Height="100"
            Fill="Blue">

            <Rectangle.Triggers>
                <!-- Animates the rectangle's opacity. -->
                <EventTrigger RoutedEvent="Rectangle.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetName="MyImage" 
                                Storyboard.TargetProperty="Opacity"
                                From="1.0" To="0.0" Duration="0:0:3" 
                                AutoReverse="True" RepeatBehavior="Forever" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Rectangle.Triggers>
        </Rectangle>
    </StackPanel>
</Grid>