MVVM子属性变化检测

发布于 2024-12-10 08:28:27 字数 2069 浏览 0 评论 0原文

我的问题如下:

我正在使用 MVVM 模式,我想知道如何检测子属性的更改。

我有一个文本框:

<TextBox Name="Descripcion" Text="{Binding AccionActual.Descripcion,Mode=TwoWay}" />

在 ViewModel 中,我有属性:

Accion _accionActual;
public Accion AccionActual
    {
        get { return _accionActual; }
        set
        {
             _accionActual = value;
             RaisePropertyChanged("AccionActual");
        }
    }

Accion 实体定义是:

public partial class Accion : Entity
    {
        public Accion()
        {

            this.AccionesDocumentos = new HashSet<AccionDocumento>();

        }

        public int IdAccion { get; set; }
        public int IdEmpleado { get; set; }
        public string Descripcion { get; set; }
        public string DescripcionDetalle { get; set; }
        public bool Finalizada { get; set; }
        public Nullable<int> IdExpediente { get; set; }
        public Nullable<int> IdOrdenTrabajo { get; set; }
        public bool Facturable { get; set; }
        public Nullable<short> GestComAlbaranAño { get; set; }
        public Nullable<short> GestComAlbaranEmpresa { get; set; }
        public Nullable<int> GestComAlbaranNumero { get; set; }
        public bool Facturado { get; set; }
        public bool ComputarHorasACliente { get; set; }
        public string DescripcionInterna { get; set; }

        public virtual Aplicacion Aplicacione { get; set; }
        public virtual AplicacionModulo AplicacionesModulo { get; set; }
        public virtual Cliente Cliente { get; set; }
        public virtual ClienteContacto ClientesContacto { get; set; }
        public virtual Empleado Empleado { get; set; }
        public virtual Expediente Expediente { get; set; }
        public virtual OrdenTrabajo OrdenesTrabajo { get; set; }
        public virtual ICollection<AccionDocumento> AccionesDocumentos { get; set; }


    }

我可以在 ViewModel 中为 Accion 的每个属性创建一个属性,但是有任何方法可以接收更改,而不必为 Accion 创建属性Accion 的每个属性?

My problem is as follows:

I am working with MVVM pattern and I would like to know how to detect changes of subproperties.

I have a textbox:

<TextBox Name="Descripcion" Text="{Binding AccionActual.Descripcion,Mode=TwoWay}" />

In the ViewModel I have the property:

Accion _accionActual;
public Accion AccionActual
    {
        get { return _accionActual; }
        set
        {
             _accionActual = value;
             RaisePropertyChanged("AccionActual");
        }
    }

The Accion entity definition is:

public partial class Accion : Entity
    {
        public Accion()
        {

            this.AccionesDocumentos = new HashSet<AccionDocumento>();

        }

        public int IdAccion { get; set; }
        public int IdEmpleado { get; set; }
        public string Descripcion { get; set; }
        public string DescripcionDetalle { get; set; }
        public bool Finalizada { get; set; }
        public Nullable<int> IdExpediente { get; set; }
        public Nullable<int> IdOrdenTrabajo { get; set; }
        public bool Facturable { get; set; }
        public Nullable<short> GestComAlbaranAño { get; set; }
        public Nullable<short> GestComAlbaranEmpresa { get; set; }
        public Nullable<int> GestComAlbaranNumero { get; set; }
        public bool Facturado { get; set; }
        public bool ComputarHorasACliente { get; set; }
        public string DescripcionInterna { get; set; }

        public virtual Aplicacion Aplicacione { get; set; }
        public virtual AplicacionModulo AplicacionesModulo { get; set; }
        public virtual Cliente Cliente { get; set; }
        public virtual ClienteContacto ClientesContacto { get; set; }
        public virtual Empleado Empleado { get; set; }
        public virtual Expediente Expediente { get; set; }
        public virtual OrdenTrabajo OrdenesTrabajo { get; set; }
        public virtual ICollection<AccionDocumento> AccionesDocumentos { get; set; }


    }

I could create in the ViewModel a property for each of the properties of Accion, but there any way to receive the changes without having to create a property for each of the properties of Accion?

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

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

发布评论

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

评论(2

疯了 2024-12-17 08:28:27

您有两种选择 - 修改 Accion 类以实现 INotifyPropertyChanged 或创建 ViewModel 包装器来执行此操作。

你把它放在哪里取决于你——做最适合你的事情。有一个关于在 ViewModel 和 ViewModel 中执行此操作的优点的问题此处为模型类。

您可以通过查看诸如 notifypropertyweaver 之类的内容来完成此操作 - 尝试使用 Google 来查看用于 INotifyPropertyChanged 面向方面的编程。这里有一个 Stackoverflow 关于它的问题

You have two choices- either modify the Accion class to implement INotifyPropertyChanged or create a ViewModel wrapper to do it.

Where you put this is up to you- do what works best for you. There is a question on the merits of doing it in the ViewModel vs Model class here.

You could take out the manual process of doing this by looking into something like notifypropertyweaver- try using Google to look for INotifyPropertyChanged Aspect Oriented Programming. There is a Stackoverflow question on it here.

残花月 2024-12-17 08:28:27

ViewModel 的这种冗余双重包装是经典 MVVM 中的常见问题,也让我抓狂。

您有多种选择:

  1. 让您的实体实现 INotifyPropertyChanged 并将实体公开给视图,就像使用 AccionActual 属性那样。
  2. 将您的实体完全隐藏在相应的 ViewModel 对象后面,并仅将那些您在视图中实际需要的属性添加到 ViewModel 中。引入一个巧妙的更改通知基础结构,通知您的 ViewModel 有关模型中的更改,并相应地在 ViewModel 中引发 PropertyChanged。这个“更改通知基础设施”可能是一个EventAggregator,也许你可以摆脱某种批量/meta update(例如,当您收到“实体已更改”事件时,为 ViewModel 中的所有相关属性引发 NotifyPropertyChanged。

This kind of redundant double wrapping by the ViewModel is a common problem in classic MVVM and drives me nuts too.

You have several options:

  1. Have your entity implement INotifyPropertyChanged and expose the Entity to the View the way you did it with the AccionActual property.
  2. Hide your Entity completely behind a corresponding ViewModel object, and add only those properties to the ViewModel that you actually need in the View. Introduce a clever change notification infrastructure that notifies your ViewModel about changes in the Model and raise PropertyChanged in your ViewModel accordingly. This "change notifcation infrastructure" could be an EventAggregator and maybe you can get away with some sort of bulk/meta update (e.g. raise NotifyPropertyChanged for all relevant properties in the ViewModel when you received the event "the entity changed".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文