更新DataGridCell的绑定值时创建动画

时间:2013-08-07 22:58:58

标签: 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>
       <DataGridTextColumn Header="KeyIndex" Binding="{Binding KeyIndex}" IsReadOnly="True"/>
       <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列。当重新编号发生时,我想优雅地刷新更新的单元格,让用户知道然后更新这些值。

我仍然是WPF和MVVM的新手,我不确定如何“倾听”这种价值变化。我的第一个想法是,我不需要新的DependencyProperty来完成这项工作,并且我可以使用SourceUpdated仅仅挂钩DataTrigger属性,但目前尚不清楚如何做这个。我试图定义以下内容

<Style x:Key="readOnlyCellUpdatedStyle" 
       TargetType="{x:Type DataGridCell}" 
       BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Style.Triggers>
        <DataTrigger Binding="ContentUpdated" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         To="1" 
                                         Duration="0:0:1" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         To="0.25" 
                                         Duration="0:0:1" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

但是,ViewModel中每个项目绑定到ContentUpdated属性远非理想。 做我想做的事的正确方法是什么?

感谢您的时间。

1 个答案:

答案 0 :(得分:2)

SourceUpdated我过去是如何做到这一点的,但您需要确保在NotifyOnSourceUpdated上将Binding设置为true。类似的东西:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Template>
        <DataTemplate>
            <TextBlock Text="{Binding KeyIndex, NotifyOnSourceUpdated=True, Mode=OneWay}">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Style.Triggers>
                            <EventTrigger RoutedEvent="Binding.SourceUpdated">
                                ...
                            </EventTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.Template>
</DataGridTemplateColumn>