首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为PropertyPath内的动画指定GroupStyle.HeaderTemplate语法

为PropertyPath内的动画指定GroupStyle.HeaderTemplate语法
EN

Stack Overflow用户
提问于 2014-09-02 11:19:16
回答 2查看 418关注 0票数 0

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

代码语言:javascript
复制
<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>

这里的结果应该是,组标题应该只为“三”组而不是任何其他组旋转。但是,运行此操作将导致以下异常

代码语言:javascript
复制
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尝试了以下几种方法,但都没有成功

代码语言:javascript
复制
(TextBlock.RenderTransform).(RotateTransform.Angle)
(RenderTransform).(Angle)
RenderTransform.Angle

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-02 11:52:01

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

代码语言:javascript
复制
Storyboard.TargetProperty="RenderTransform.(RotateTransform.Angle)"

我只用TextBlock测试了它,可以看到它在DataGrid中旋转,所以如果您看不到它在您的DataGrid中旋转,那么您的Name属性Binding是不正确的,或者它的值不是预期的"Three"

代码语言:javascript
复制
<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.(RotateTransform.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>
票数 1
EN

Stack Overflow用户

发布于 2014-09-02 11:25:55

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

代码语言:javascript
复制
<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>

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25622167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档