我有一个像下面图片中那样的列表视图。正如您所看到的,我有一个名为时间的列。
我想知道如何自动更新时间。
例如,航班代码ABC123的时间在1分钟后应从3分钟更改为4分钟。
例如,航班代码ABC123的时间在1分钟后应从3分钟更改为4分钟。
编辑并不满意每分钟使绑定失效的想法(这可能是完全没有根据的,每分钟使其失效也没问题,我不知道),因此提出了一种更“纯净”的实现方式,直接设置内容,但为了参考保留了原始内容:
您可以通过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;
}
}
<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;
}
}
<Label Content="{Binding Path=InitialisedDate, Converter={StaticResource cnvTimeAgo}}" local:PropertyToInvalidateBehaviour.PropertyToInvalidate="Label.Content" />`
查看 调度计时器并每分钟执行一次记录更新
这就是我会做的
大而聪明的ViewModel、愚蠢的View和任何模型,最好的MVVM方法? 包含WPF应用程序的代码,其中ViewModel(和与之绑定的View)由计时器更新。 它每秒钟更新一次(1000毫秒)