MVVM子属性变化检测
我的问题如下:
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两种选择 - 修改 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.
ViewModel 的这种冗余双重包装是经典 MVVM 中的常见问题,也让我抓狂。
您有多种选择:
INotifyPropertyChanged
并将实体公开给视图,就像使用AccionActual
属性那样。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:
INotifyPropertyChanged
and expose the Entity to the View the way you did it with theAccionActual
property.