我有一个如下图所示的列表视图。你可以看到我有一个名为时间的专栏。 我想知道如何自动更新时间。
例如,飞行代码ABC123的时间应在1分钟后从3分钟变为4分钟。
答案 0 :(得分:1)
查看dispatch timer并每分钟执行一次记录更新
这就是我要做的事情
答案 1 :(得分:1)
Big smart ViewModels, dumb Views, and any model, the best MVVM approach?包含WPF应用程序的代码,其中ViewModel(和绑定到它的View)由计时器更新。它是每1秒(1000毫秒)
答案 2 :(得分: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" />`