绑定到时间依赖属性 [英] Binding to time-dependent properties

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

问题描述

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

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

显然理论上这个属性会改变每一个勾号,你想更新你的UI每一次(例如,通过定期抽出一个 PropertyChanged 事件为该属性。)



该小部件我每分钟刷新整个任务列表,但是这是不太理想的,因为如果用户与某些项目进行交互(例如,通过键入绑定到评论的TextBox)属性),将被严格地中断,更新源将丢失。



所以如果您有这样的时间依赖属性,那么更新UI的最佳方法可能是什么



我不会再使用该应用程序了,只是认为这是一个非常有趣的问题)

解决方案

我认为你的第一个段落中所说的代码示例是WPF中唯一合理的方法。设置一个计时器,只需为 TimeLeft 属性调用 PropertyChanged 间隔将根据您的情况而有所不同(如果您正在谈论每周任务列表,则可能只需要5分钟更新)如果您在接下来的30分钟内谈论任务列表,则可能需要更新它每分钟或30秒或某事。



该方法将避免您提到的刷新选项的问题,因为只有 TimeLeft 绑定会受到影响,如果你有这么多的这些任务,我猜这个性能的惩罚是相当重要的,但是如果你只有几十个,那么每30秒钟更新一次绑定就是相当微不足道的问题,对吗?



我可以想到的每一种可能性都会使用计时器或动画。动画会在列表中添加任务时太沉重而在Timer方案中,上面的一个似乎是最干净,最简单和最实用的,可能只是归结于它是否适用于你的specifi c情景。


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; }
}

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).

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.

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

(I don't use that application anymore by the way, just thought this was a very interesting question)

解决方案

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.

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?

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天全站免登陆