为类/结构的所有成员引发 INotifyPropertyChanged?

发布于 2024-10-12 00:40:39 字数 782 浏览 1 评论 0原文

所以我有一个像这样设置的项目

Class Dataclass
{
  var data
  var data;
  var data;
 }

class DataViewModel
{
   private Dataclass thing;

   public string thingPropertyName = "thing";
   public Dataclass Thing
   {
     get { return thing; }
     set { thing = value; RaisedPropertyChanged(thingPropertyName);}
   }
 }

所以这是我的问题:如果我通过 ModelView 类中的 Thing 字段绑定到子属性,则如果该类的任何成员发生更改,则绑定不会更新,只有当整个物体发生了变化,我认为这是故意的。

我怎样才能解决这个问题,以便如果我对所包含的数据类的成员执行任何操作,我的绑定都会正确更新?

我首先考虑的是将子类的每个元素包装到父类的属性中,如下所示:

public var Data
{
   get { return thing.Data; }
   set { thing.Data = value; RaisePropertyChanged(DataProperyName);}
}

这可行,但是在初始化子类对象时我仍然遇到问题。我能看到的唯一解决方案是在初始化类时为每个元素手动调用 RaisingPropertyName() 。这似乎不是最佳的实现方式。我希望有更好的方法。谢谢。

So I've got a project set up like this

Class Dataclass
{
  var data
  var data;
  var data;
 }

class DataViewModel
{
   private Dataclass thing;

   public string thingPropertyName = "thing";
   public Dataclass Thing
   {
     get { return thing; }
     set { thing = value; RaisedPropertyChanged(thingPropertyName);}
   }
 }

So here is my issue: If I bind to a subproperty via the Thing field in my ModelView class, the bindings do not update if any of the members of the class change, only if the entire object changed, as what I believe is intentional.

How can I get around this, so that if I do anything to the members of the contained Dataclass, my bindings update properly?

The first thing I'd looked at was was wrapping every element of the subclass into a property of the parent class, like so:

public var Data
{
   get { return thing.Data; }
   set { thing.Data = value; RaisePropertyChanged(DataProperyName);}
}

This works, however I still have an issue when the subclass object is initialized. The only solution I can see is to manually call RaisedPropertyName() for each element when the class is initialized. This seems like a less than optimal way to do it. I was hoping that there would be a better approach. Thanks.

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

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

发布评论

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

评论(2

屌丝范 2024-10-19 00:40:39

在大多数情况下,调用 RaisePropertyChanged(null)(假设这最终会调用相应的 INotifyPropertyChanged 事件委托,并将 null 作为 args 结构中的属性名称参数)将告诉所有绑定侦听器进行刷新。

In most cases, calling RaisePropertyChanged(null) (assuming that this ultimately invokes a call to the appropriate INotifyPropertyChanged event delegate with null as the property name parameter in the args structure) will tell all bound listeners to refresh.

终弃我 2024-10-19 00:40:39

如果您要绑定到 DataClass 的属性,并且希望刷新绑定(这是使用绑定的最终目标),那么这意味着您的 DataClass 必须实现 INotifyPropertyChanged (其目的正是如此)。

如果您不希望 DataClass 实现 INotifyPropertyChanged,出于“层”原因(例如:您不希望您的 Data 类实现与视图相关的关注点),那么您的 DataViewModel 必须充当您所使用的每个属性的传递。需要。这是您提出的第二个解决方案。

If you are binding to a property of DataClass, and want the binding to be refreshed (which is ultimately the goal of using bindings), then it means your DataClass must implement INotifyPropertyChanged (whose purpose is exactly that).

If you don't want DataClass to implement INotifyPropertyChanged, for "layers" reasons (e.g.: you don't want your Data classes to implement concerns related to Views), then your DataViewModel must act as a pass-through for each property that you need. This is the second solution that you propose.

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