如何创建自定义数据网格

本文关键字:自定义 数据 数据网 网格 创建 何创建 | 更新日期: 2023-09-27 18:27:19

所有,在这个答案中https://stackoverflow.com/a/18136371/626442对于前面的一个问题,这个答案为我的动画不开火的问题提供了解决方案。代码是

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

这将在我通过ViewModel更新KeyIndex列时激发动画。但是,我只想在更新值时为DataGridTextColumn的单元设置动画,但在滚动DataGrid时也会激发它们。有人建议我

创建具有所需内置功能的派生DataGridTextColumn可能是一个更好的选择,因为如果使用虚拟化,则使用TargetUpdated几乎是不可能的。但是,如果你制作了一个自定义的DataGridTextColumn,你应该能够让它工作。

如何创建自定义/派生`DataGridTextColumn(我也有权混合)

我不知道如何做到这一点,关于这个特定概念的文档也很少。

谢谢你抽出时间。

如何创建自定义数据网格

当使用VirtualizationMode="Recycling" 时,使用Virtualization很难做到这一点

我找到了一种方法,通过派生TextBlock并附加到绑定对象的PropertyChanged事件来使其工作。

当值更改时,将播放动画,但如果滚动,则回收项目不会继续播放动画。

这是一个非常粗略的例子,但我相信你可以根据自己的需要进行清理和修改。

文本块:

public class VirtualizingNotifyTextBlock : TextBlock
{
    private INotifyPropertyChanged _dataItem;
    private string _propertyName;
    public VirtualizingNotifyTextBlock()
        : base()
    {
        this.TargetUpdated += NotifyTextBlock_TargetUpdated;
    }
    public static readonly RoutedEvent PropertyChangedEvent = EventManager.RegisterRoutedEvent("PropertyChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(VirtualizingNotifyTextBlock));
    public static readonly DependencyProperty NotifyDurationProperty = DependencyProperty.Register("NotifyDuration", typeof(int), typeof(VirtualizingNotifyTextBlock), new PropertyMetadata(300));
    public static readonly DependencyProperty NotifyRepeatProperty = DependencyProperty.Register("NotifyRepeat", typeof(int), typeof(VirtualizingNotifyTextBlock), new PropertyMetadata(3));
    public static readonly DependencyProperty NotifyColorProperty = DependencyProperty.Register("NotifyColor", typeof(Color), typeof(VirtualizingNotifyTextBlock), new PropertyMetadata(Colors.Red));
    public Color NotifyColor
    {
        get { return (Color)GetValue(NotifyColorProperty); }
        set { SetValue(NotifyColorProperty, value); }
    }
    public int NotifyDuration
    {
        get { return (int)GetValue(NotifyDurationProperty); }
        set { SetValue(NotifyDurationProperty, value); }
    }
    public int NotifyRepeat
    {
        get { return (int)GetValue(NotifyRepeatProperty); }
        set { SetValue(NotifyRepeatProperty, value); }
    }
    public INotifyPropertyChanged DataItem
    {
        get { return _dataItem; }
        set
        {
            if (_dataItem != null)
            {
                Background = new SolidColorBrush(Colors.Transparent);
                _dataItem.PropertyChanged -= DataItem_PropertyChanged;
            }
            _dataItem = value;
            if (_dataItem != null)
            {
                _dataItem.PropertyChanged += DataItem_PropertyChanged;
            }
        }
    }
    private void NotifyTextBlock_TargetUpdated(object sender, DataTransferEventArgs e)
    {
        var binding = this.GetBindingExpression(VirtualizingNotifyTextBlock.TextProperty);
        if (binding != null)
        {
            _propertyName = binding.ResolvedSourcePropertyName;
            DataItem = binding.DataItem as INotifyPropertyChanged;
        }
    }
    private void DataItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == _propertyName)
        {
           var animation = new ColorAnimation(NotifyColor, new Duration(TimeSpan.FromMilliseconds(NotifyDuration)));
           animation.RepeatBehavior = new RepeatBehavior(NotifyRepeat);
           animation.AutoReverse = true;
           Background = new SolidColorBrush(Colors.Transparent);
           Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);
        }
    }
}

Xaml:

<DataTemplate x:Key="readOnlyCellUpdatedStyle">
    <local:VirtualizingNotifyTextBlock Text="{Binding KeyIndex, NotifyOnTargetUpdated=True}" NotifyColor="Blue" NotifyDuration="250" NotifyRepeat="4" />
</DataTemplate>