WPF动画没有被解雇

时间:2013-08-08 20:57:05

标签: c# wpf animation mvvm datagrid

我只有以下DataGrid

<DataGrid x:Name="resourceDataGrid" 
          HorizontalAlignment="Stretch" 
          VerticalAlignment="Stretch" 
          AutoGenerateColumns="false" 
          GridLinesVisibility="None"
          RowHeaderWidth="0" 
          CanUserAddRows="True" 
          CanUserDeleteRows="True" 
          ItemsSource="{Binding Path=Resources, 
                                Mode=TwoWay,
                                UpdateSourceTrigger=PropertyChanged, 
                                IsAsync=True}">
    <DataGrid.Columns>
        <DataGridTemplateColumn CellTemplate="{StaticResource readOnlyCellUpdatedStyle}"  IsReadOnly="True"/>
        <!--<DataGridTextColumn Header="KeyIndex" Binding="{Binding KeyIndex}" IsReadOnly="True"/>--> <- What I did have...
        <DataGridTextColumn Header="FileName" Binding="{Binding FileName}" IsReadOnly="True"/>
        <DataGridTextColumn Header="ResourceName" Binding="{Binding ResourceName}" IsReadOnly="False"/>
        <controls:CollectionTextColumn Collection="ResourceStringList" Visibility="Collapsed"/>
    </DataGrid.Columns>
</DataGrid>

当删除数据集中的某行时,我想重新编号KeyIndex列。当重新编号发生时,我想优雅地刷新更新的单元格,让用户知道然后更新这些值。

我创建了以下DataTemplate

<DataTemplate x:Key="readOnlyCellUpdatedStyle">
    <TextBlock Text="{Binding KeyIndex, NotifyOnSourceUpdated=True, Mode=TwoWay}">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Binding.SourceUpdated">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="Background.Color" 
                                                Duration="0:0:0.3"
                                                From="White" 
                                                To="Red" 
                                                RepeatBehavior="3x" 
                                                AutoReverse="True"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</DataTemplate>

绑定仍然适用于KeyIndex,但是当我从ViewModel更新KayIndex值时,现在动画就会发生变化。 为什么KeyIndex更新时动画未触发?

感谢您的时间。

1 个答案:

答案 0 :(得分:2)

您需要使用NotifyOnTargetUpdatedBinding.TargetUpdated。并且不要忘记先设置背景值,否则设置颜色会引发异常。

  • Binding.SourceUpdated元素 - &gt;对象
  • Binding.TargetUpdated对象 - &gt;元件

有时可能会非常混乱

<DataTemplate x:Key="readOnlyCellUpdatedStyle">
    <TextBlock Text="{Binding KeyIndex, Mode=TwoWay,NotifyOnTargetUpdated=True}">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Background" Value="White"/>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Binding.TargetUpdated">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="Background.Color" 
                                    Duration="0:0:0.3"
                                    From="White" 
                                    To="Red" 
                                    RepeatBehavior="3x" 
                                    AutoReverse="True"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</DataTemplate>