WPF MVVM - 可以单个PropertyChanged更新DataTemplate的所有Data绑定? [英] WPF MVVM - Can a single PropertyChanged update all the Data bindings of a DataTemplate?

查看:113
本文介绍了WPF MVVM - 可以单个PropertyChanged更新DataTemplate的所有Data绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有大量属性的ViewModel类(Say 50)。一旦将数据设置为我需要更新UI的所有属性。我知道常见的解决方案是在所有属性设置器上调用PropertyChanged 。



我想知道是否有任何方法可以通过我的DataTemplate通过一个通知来更新所有的绑定?一个想法是有一个IsLoaded属性引发了propertieschanged,但是如何使用它来更新整个DataTemplate。我感兴趣的是一个完整的XAML解决方案。

解决方案

最后我得到了我的同事 Josh Smith ,这很简单,我们只需要用 null或String.Empty 作为属性名称。这将告诉WPF绑定系统重新评估该对象的所有绑定。在使用这个过程中,我获得了两个主要的优势。


  1. 在每个属性上提升个别事件的开销将减少到只有一个Event-模型到UI。演出
    将会增加。

  2. 在编写代码时,我们可以使用非常干净的{get; set;}语法,而
    写入

假设:正如我在这里假设的一个非常特殊的条件,我的所有属性都在同一时间更新



我有一个 blogpost在这里讨论



更新:根据kek444的评论


I have a ViewModel class which has large number of properties(Say 50). Once the data is set on to all the properties I need to update the UI. I know that the common solution is to raise PropertyChanged on all the property setters.

I am wondering if there is any way I can notify my DataTemplate to update all its bindings through a single notification? One idea is to have a IsLoaded property which raises the propertychanged but how can I use that to update the entire DataTemplate. I am interested in a total XAML solution to this.

解决方案

Finally I have got an answer from my colleague Josh Smith , it is very simple, we just need to raise a PropertyChanged event with null or String.Empty as property name. which will tell the WPF binding system to re-evaluate all the bindings of that object. I am getting two major advantage while using this.

  1. The overhead of raising individual events on each property will reduce to just one Event from View-Model to the UI. Performance will get increased.
  2. While writing code we can just use {get;set;} syntax which is clean and less writing

Assumption : As in the question I assumed here that it is a very special condition in which all my Properties are getting updated at the same time

I have a blogpost discussing this here

Update: based on the comment from kek444

这篇关于WPF MVVM - 可以单个PropertyChanged更新DataTemplate的所有Data绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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