C# WPF Listview 更新

3
我有一个像下面图片中那样的列表视图。正如您所看到的,我有一个名为时间的列。 我想知道如何自动更新时间。
例如,航班代码ABC123的时间在1分钟后应从3分钟更改为4分钟。
3个回答

1

编辑并不满意每分钟使绑定失效的想法(这可能是完全没有根据的,每分钟使其失效也没问题,我不知道),因此提出了一种更“纯净”的实现方式,直接设置内容,但为了参考保留了原始内容:

您可以通过AttachedBehaviour来实现它,以下内容应该能够帮助您入门:

public static class TimeAgoBehaviour {

    static Lazy<DispatcherTimer> LazyTimer = new Lazy<DispatcherTimer>(() => {
        DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMinutes(1) };
        timer.Start();
        return timer;
    });
    static ConcurrentDictionary<int, EventHandler> Events = new ConcurrentDictionary<int, EventHandler>();

    public static DateTime GetTimeAgo(ContentControl obj) {
        return (DateTime)obj.GetValue(TimeAgoProperty);
    }

    public static void SetTimeAgo(ContentControl obj, DependencyProperty value) {
        obj.SetValue(TimeAgoProperty, value);
    }

    public static readonly DependencyProperty TimeAgoProperty = DependencyProperty.RegisterAttached("TimeAgo",
        typeof(DateTime), typeof(TimeAgoBehaviour),
        new UIPropertyMetadata(DateTime.UtcNow, OnTimeAgoChanged));

    private static void OnTimeAgoChanged(object sender, DependencyPropertyChangedEventArgs e) {
        var newDate = (DateTime)e.NewValue;

        EventHandler oldEvent;
        if (Events.TryRemove(sender.GetHashCode(), out oldEvent)) {
            LazyTimer.Value.Tick -= oldEvent;
        }

        if (DateTime.MinValue == newDate) {
            return;
        }

        var doUpdate = new EventHandler((s, args) => {
            ContentControl control = sender as ContentControl;
            control.Content = TimeAgoConverter.GetTimeAgo(newDate);
        });

        doUpdate(sender, new EventArgs());

        Events.TryAdd(sender.GetHashCode(), doUpdate);
        LazyTimer.Value.Tick += doUpdate;
    }
}

Xaml:
<Label local:TimeAgoBehaviour.TimeAgo="{Binding InitialisedDate}" />

以下是原始响应:
您可以通过AttachedBehaviour来实现,每分钟使绑定失效,导致其被重新评估。以下代码可以作为起点,在需要的地方进行扩展:

代码:

public static class PropertyToInvalidateBehaviour {

    static Lazy<DispatcherTimer> LazyTimer = new Lazy<DispatcherTimer>(() => {
        DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMinutes(1) };
        timer.Start();
        return timer;
    });
    static ConcurrentDictionary<int, EventHandler> Events = new ConcurrentDictionary<int, EventHandler>();

    public static DependencyProperty GetPropertyToInvalidate(DependencyObject obj) {
        return (DependencyProperty)obj.GetValue(PropertyToInvalidateProperty);
    }

    public static void SetPropertyToInvalidate(DependencyObject obj, DependencyProperty value) {
        obj.SetValue(PropertyToInvalidateProperty, value);
    }

    public static readonly DependencyProperty PropertyToInvalidateProperty = DependencyProperty.RegisterAttached("PropertyToInvalidate",
      typeof(DependencyProperty), typeof(PropertyToInvalidateBehaviour),
      new UIPropertyMetadata(null, OnPropertyToInvalidateChanged));

    private static void OnPropertyToInvalidateChanged(object sender, DependencyPropertyChangedEventArgs e) {
        var propertyToInvalidate = e.NewValue as DependencyProperty;

        EventHandler oldEvent;
        if (Events.TryRemove(e.Property.GetHashCode(), out oldEvent)) {
            LazyTimer.Value.Tick -= oldEvent;
        }

        if (null == propertyToInvalidate) {
            return;
        }

        var doUpdate = new EventHandler((s, args) => {
            BindingExpression binding = BindingOperations.GetBindingExpression(sender as DependencyObject, propertyToInvalidate);
            binding.UpdateTarget();
        });

        Events.TryAdd(e.Property.GetHashCode(), doUpdate);
        LazyTimer.Value.Tick += doUpdate;
    }
}

Xaml:
<Label Content="{Binding Path=InitialisedDate, Converter={StaticResource cnvTimeAgo}}" local:PropertyToInvalidateBehaviour.PropertyToInvalidate="Label.Content" />`

1

查看 调度计时器并每分钟执行一次记录更新

这就是我会做的


是否可以通过事件或委托来解决这个问题? - EM10
当然可以,但是你打算绑定到哪个事件上呢?如果你需要每分钟执行一次,我认为这是最好的方法。 - J King

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接