3

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136

2 Answers2

2

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.

Community
  • 1
  • 1
RichardOD
  • 28,883
  • 9
  • 61
  • 81
  • either way @user1001552 will have to raise property changed event for each member and for that he/she has to implement a property for each member of the class Accion – smile.al.d.way Oct 18 '11 at 17:07
0

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".
bitbonk
  • 48,890
  • 37
  • 186
  • 278