当DataGridCell';s的绑定值已更新

本文关键字:绑定 已更新 DataGridCell | 更新日期: 2023-09-27 18:26:52

所有我都有以下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来完成这项工作,只需使用DataTrigger挂接到SourceUpdated属性就可以完成,但不清楚如何做到这一点。我试图定义以下

<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属性远非理想做我想做的事的正确方法是什么

谢谢你抽出时间。

当DataGridCell';s的绑定值已更新

SourceUpdated是我过去的做法,但您需要确保在Binding上将NotifyOnSourceUpdated设置为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>