1

我有以下所有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但是当我KayIndex从 ViewModel 更新值时,动画会发生,值只会发生变化。为什么更新时动画不触发KeyIndex

谢谢你的时间。

4

1 回答 1

2

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

  • Binding.SourceUpdated元素 -> 对象
  • Binding.TargetUpdated对象 -> 元素

有时会变得非常混乱

<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>
于 2013-08-08T21:21:23.937 回答