绑定到时间相关的属性 [英] Binding to time-dependent properties

查看:13
本文介绍了绑定到时间相关的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我写了一个类似小部件的应用程序,它应该跟踪任务,每个任务都有一个指定为 DateTime 的截止日期,现在如果你想显示有多少时间直到截止日期,您可能想要绑定到虚拟"(*诅咒 virtual 关键字*)属性,如下所示:

Some time ago i wrote a small widget-like application which was supposed to keep track of tasks, each task had a deadline specified as a DateTime, now if you want to display how much time is left until the deadline you might want to bind to a "virtual" (*curses the virtual keyword*) property like this:

public TimeSpan TimeLeft
{
    get { return Deadline - DateTime.Now; }
}

从理论上讲,这个属性显然会在每个滴答声中发生变化,并且您希望时不时地更新您的 UI(例如,通过定期为该属性抽出 PropertyChanged 事件).

Obviously in theory this property changes every tick and you want to update your UI every now and then (e.g. by periodically pumping out a PropertyChanged event for that property).

当我编写小部件时,我每分钟刷新整个任务列表,但这并不理想,因为如果用户与某些项目交互(例如,通过键入绑定到 Comments 的 TextBox-property) 将被严重中断并且源更新丢失.

Back when i wrote the widget i refreshed the whole task list every minute, but this is hardly ideal since if the user interacts with some item (e.g. by typing in a TextBox which binds to a Comments-property) that will be harshly interupted and updates to the source get lost.

如果您有这样的时间相关属性,那么更新 UI 的最佳方法是什么?

So what might be the best approach to updating the UI if you have time-dependent properties like this?

(顺便说一句,我不再使用那个应用程序了,只是觉得这是一个非常有趣的问题)

推荐答案

我认为您在代码示例之后的第一段中所说的是在 WPF 中使其工作的唯一合理方法.设置一个只为 TimeLeft 属性调用 PropertyChanged 的计时器.间隔会根据您的情况而有所不同(如果您正在谈论每周任务列表,您可能只需要每 5 分钟左右更新一次.如果您正在谈论接下来 30 分钟的任务列表,您可能需要每分钟或 30 秒更新一次.

I think what you said in your first paragraph after the code sample is the only reasonable way to make this work in WPF. Set up a timer that Just calls PropertyChanged for the TimeLeft property. The interval would vary based upon your scenario (if you're talking a weekly task list, you probably only need to update it ever 5 minutes or so. If you're talking a task list for the next 30 minutes, you may need to update it every minute or 30 seconds or something.

该方法将避免您提到的刷新选项问题,因为只有 TimeLeft 绑定会受到影响.如果你有数百万个这样的任务,我想性能损失会相当大.但是,如果您只有几十个或其他东西,那么每 30 秒左右更新一次这些绑定将是一个非常微不足道的问题,对吧?

That method would avoid the problems you mentioned with the refresh option since only the TimeLeft bindings would be affected. If you had millions of these tasks, I guess the performance penalty would be pretty significant. But if you only had a few dozen or something, updating those bindings every 30 seconds or so would be be a pretty insignificant issue, right?

我能想到的每一种可能性都使用计时器或动画.当您将任务添加到列表时,动画会太沉重".在 Timer 场景中,上面的那个似乎是最干净、最简单和最实用的.可能只是归结为它是否适用于您的特定场景.

Every possibility that I can think of uses either Timers or Animations. The animations would be way too "heavy" as you add tasks to the list. And of the Timer scenarios, the one above seems to be the cleanest, simplest and most practical. Probably just comes down to whether it even works or not for your specific scenario.

这篇关于绑定到时间相关的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆