我目前使用反射来获取特定类型或访问级别的类中的所有属性名称。然后,我通过 RaisePropertyChanged() 运行它们以实质上“更新”整个 View 。
我执行此操作的一个实例是在启动时,当程序启动并且实例化 ViewModel 时,它将运行它以确保 View 显示模型中的所有正确数据。
这样做有什么问题吗?
代码,如果你们想要的话:
private void InitializeViewModel()
{
foreach (string name in MiscMethods.GetPropertyNames(this))
{
RaisePropertyChanged(name);
}
}
public static IEnumerable<string> GetPropertyNames(Object yourClass)
{
foreach (PropertyInfo property in GetProperties(yourClass))
{
yield return property.Name;
}
}
//Uses Reflection to return all properties in a class
private static IEnumerable<PropertyInfo> GetProperties(Object theObject)
{
return theObject.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance);
}
最佳答案
带空字符串的上升事件
RaisePropertyChanged("");
这将触发对所有 属性的更新。
关于c# - 当我需要 "refresh"或 "update" View 时,为我的 ViewModel 中的每个属性调用 RaisePropertyChanged() 是不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31719215/