【问题标题】:MVVM subproperty change detectionMVVM 子属性更改检测
【发布时间】:2011-12-10 06:50:09
【问题描述】:

我的问题如下:

我正在使用 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 的每个属性创建一个属性?

【问题讨论】:

    标签: mvvm entity


    【解决方案1】:

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

    你把它放在哪里取决于你 - 做最适合你的事情。有a question on the merits of doing it in the ViewModel vs Model class here

    您可以通过查看 notifypropertyweaver 之类的内容来取消执行此操作的手动过程 - 尝试使用 Google 查找 INotifyPropertyChanged Aspect Oriented Programming。有一个 Stackoverflow question on it here

    【讨论】:

    • 无论哪种方式,@user1001552 都必须为每个成员引发属性更改事件,为此他/她必须为 Accion 类的每个成员实现一个属性
    【解决方案2】:

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

    您有多种选择:

    1. 让您的实体实现INotifyPropertyChanged,并按照您使用AccionActual 属性的方式将实体公开给视图。
    2. 将您的 Entity 完全隐藏在相应的 ViewModel 对象后面,并仅将您在视图中实际上需要的那些属性添加到 ViewModel。引入一个聪明的更改通知基础设施,通知您的 ViewModel 模型中的更改,并相应地在您的 ViewModel 中引发 PropertyChanged。这个“更改通知基础设施”可能是EventAggregator,也许您可​​以通过某种批量/元更新(例如,当您收到“实体已更改”事件时,为 ViewModel 中的所有相关属性提高 NotifyPropertyChanged )。李>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-18
      • 2013-12-02
      • 2014-05-15
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2021-08-05
      • 2012-07-24
      相关资源
      最近更新 更多