TextBox UpdateSourceTrigger = PropertyChanged - 它真的会影响性能吗?

发布于 2024-11-17 18:08:35 字数 683 浏览 4 评论 0原文

MSDN 文档指出:

TwoWay 或 OneWayToSource 监听变化 目标属性并传播它们 回到源头。这被称为 更新来源。通常,这些 每当目标发生更新 属性变化。这对于 复选框和其他简单的控件, 但通常不适合 文本字段。 每更新一次 击键会降低性能并且 它拒绝用户通常的 退格和修复的机会 在提交之前输入错误 新的价值。因此,默认 文本的 UpdateSourceTrigger 值 属性是 LostFocus 而不是 属性已更改。

据我所知,在更新直接进入数据库或通过网络,或者数据量极大的情况下,在 TextBox 上使用 UpdateSourceTrigger = PropertyChanged 确实可能会降低性能。

但是,如果它只是更新一个简单的 DependencyProperty 或实体框架对象的属性(在提交之前),那么性能影响是否可以忽略不计?

只是想知道,因为我正在创建一个 WPF 应用程序,它跟踪正在编辑的对象的状态,并根据是否进行了更改来优化“保存”按钮的外观。我认为确定更改的最简单方法是适当捕获相关的 SourceUpdated 事件。当文本框的 UpdateSourceTrigger = PropertyChanged 时,它的工作效果最佳,因为用户会得到存在“可保存”更改的即时反馈。

The MSDN documentation states:

Bindings that are TwoWay or
OneWayToSource listen for changes in
the target property and propagate them
back to the source. This is known as
updating the source. Usually, these
updates happen whenever the target
property changes. This is fine for
check boxes and other simple controls,
but it is usually not appropriate for
text fields. Updating after every
keystroke can diminish performance
and
it denies the user the usual
opportunity to backspace and fix
typing errors before committing to the
new value. Therefore, the default
UpdateSourceTrigger value of the Text
property is LostFocus and not
PropertyChanged.

I understand that in a situation where the update is going directly to a database, or across a network, or if it's an extremely large amount of data, that it could indeed diminish performance to use UpdateSourceTrigger = PropertyChanged on TextBoxes.

But if it is just updating a simple DependencyProperty, or a property of an Entity Framework object (prior to committing), would the performance hit not be negligible?

Just wondering, because I am creating a WPF app which tracks the state of the object being edited and optimizes the Save button appearance depending on whether changes have been made. I thought the easiest way to determine changes would be to catch the relevant SourceUpdated occurences as appropriate. It works optimally when UpdateSourceTrigger = PropertyChanged for the textboxes, as the user gets instant feedback that there are "saveable" changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开浅夏 2024-11-24 18:08:35

您收到有关性能下降的警告的原因是,在大多数情况下,如果您需要在每次击键时更新源属性,那是因为您需要在属性值更改时发生一些事情。毕竟,如果您不需要发生这种“事情”,您就不会真正关心属性何时更新,只要它最终更新即可。

对性能的真正影响完全取决于那个“东西”是什么。这完全取决于您的应用程序。如果该“某事”正在格式化并显示另一个 TextBlock 中的值,则每次击键时执行此操作可能不会被注意到。如果它要过滤 10,000 行的 DataTable 并刷新绑定到它的 DataGrid,它可能会这样做。

那么你怎么知道呢?有两种方法:

1)了解您的应用程序。如果您知道更新源属性时应用程序正在做什么,则可以预测每次击键时执行此操作是否会出现问题。当你说“我想我想知道它是否一开始看起来没问题,但实际上在我不知道的某些情况下可能会导致问题”时,你真正想说的是,“如果我不这样做会发生什么”不知道当用户按下某个键时我的应用程序正在做什么?”

2) 如果您不知道用户按下某个键时应用程序正在做什么,请对其进行分析。

The reason that you're warned about performance degradation is that for the most part, if you need to have the source property updated on every keystroke, it's because you need something to happen when the property's value changes. After all, if you didn't need that "something" to happen, you wouldn't really care when the property got updated, so long as it did eventually.

The real impact on performance depends entirely on what that "something" is. And that's totally dependent on your application. If that "something" is formatting and displaying the value in another TextBlock, doing it on every keystroke probably won't be noticeable. If it's filtering a 10,000-row DataTable and refreshing a DataGrid bound to it, it probably will.

So how do you tell? Well, there are two ways:

1) Understand your application. If you know what the application is doing when you update the source property, you can predict whether or not doing it on every keystroke is going to be a problem. When you say "I guess I was wondering whether it might seem to be fine at first, but can actually cause issues in certain situations I'm not aware of," what you're really saying is, "What happens if I don't know what my application is doing when the user presses a key?"

2) If you don't know what your application is doing when the user presses a key, profile it.

凉世弥音 2024-11-24 18:08:35

如果它适合您的应用程序并且您没有注意到性能显着下降,那么将 UpdateSourceTrigger 设置为 PropertyChanged 就没有问题。事实上,如果您使用的是 MVVM 框架,例如 Caliburn.Micro,那么它会将其设置为所有文本框的默认设置。

If it is suitable for your application and you don't notice a significant degradation in performance, then there is no problem setting the UpdateSourceTrigger to be PropertyChanged. In fact, if you're using an MVVM framework such as Caliburn.Micro, then it will set this as the default setting for all TextBoxes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文