2014-09-02 46 views
0

我想在DataGrid的GroupStyle頭部內執行旋轉動畫,並且我無法獲取Storyboard.TargetProperty屬性的PropertyPath語法。下面是一個包含例子來突出我的問題在GroupStyle.HeaderTemplate中指定動畫的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> 

這裏的結果應該是組頭應旋轉只爲「三」組,而不是任何其他人。但是,在運行這導致以下異常

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?

回答

1

您的代碼非常接近的作品,因爲它是。你只需要做出一個變化:

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

我曾經使用過的TextBlock測試它,可以看到,紡了,所以如果你不能看到它紡紗在你DataGrid,無論你Name財產Binding是不正確,或其值不是預期的"Three"

<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> 
+0

嘿,你的例子工作正常,但問題是由於它是在datagrid組頭。 Name綁定來自CollectionViewGroupInternal對象,它是GroupItem的datacontext,並且該綁定能正常工作。 – ChrisO 2014-09-02 12:01:05

+0

你是說在DataGrid裏面它*不工作嗎? – Sheridan 2014-09-02 12:19:49

+0

特別是在datagrid頭部內時,是的。 – ChrisO 2014-09-02 12:48:52

-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>