gpt4 book ai didi

c# - 如何创建自定义数据网格

转载 作者:行者123 更新时间:2023-11-30 22:15:07 25 4
gpt4 key购买 nike

所有,在这个答案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 滚动时触发。我被建议

It may be a better option to create a derived DataGridTextColumn with the features you need built in, as using TargetUpdated will be pretty much impossible if you are using Virtualization. But if you made a custom DataGridTextColumn you should be able to get it working.

如何创建自定义/派生的`DataGridTextColumn(我也可以访问混合)?

我不知道该怎么做,而且关于这个特定概念的文档很少。

感谢您的宝贵时间。

最佳答案

事实证明,在使用 VirtualizationMode="Recycling" 时,很难通过 Virtualization 实现这一点

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

当值改变时播放动画,但是如果你滚动回收的项目不会继续播放动画。

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

文本 block :

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>

关于c# - 如何创建自定义数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18174231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com