[英]nested User Control binding not working for nested property
我在C#win应用程序中具有三个用户控件; 主要用户称为UcReferenteTecnico,它仅包含具有嵌套用户控件UcIndirizzo的UcContatto。 UcContatto有一个名为ContattoMV的模型视图,UcIndirizzo有一个名为IndirizzoMV的模型视图
UcContatto模型视图具有属性和嵌套的IndirizzoMV属性; 他们是这样完成的:
public class ContattoMV:INotifyPropertyChanged
{
string _NOME_CONTATTO;
[HeaderAttribute("Nome contatto", true, 2)]
public string NOME_CONTATTO
{
get { return _NOME_CONTATTO; }
set
{
_NOME_CONTATTO = value;
NotifyPropertyChanged("NOME_CONTATTO");
}
}
public IndirizzoMV Indirizzo
{
get { return _Indirizzo; }
set
{
_Indirizzo = value;
NotifyPropertyChanged("Indirizzo");
}
}
public void NotifyPropertyChanged(string aiPropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(aiPropertyName));
}
}
}
public class IndirizzoMV:INotifyPropertyChanged
{
public string TOPONIMO
{
get { return _TOPONIMO; }
set
{
_TOPONIMO = value;
NotifyPropertyChanged("TOPONIMO");
}
}
public void NotifyPropertyChanged(string aiPropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(aiPropertyName));
}
}
}
所有属性在UcContatto和UcIndirizzo中都以这种方式绑定到Control:在UcContatto中:
this.txtNome.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsContatto, "NOME_CONTATTO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
并绑定嵌套的usercontrol UcIndirizzo,请执行以下操作:
this.ucIndirizzo1.DataBindings.Add(new System.Windows.Forms.Binding("BsIndirizzo", this._bsContatto, "Indirizzo", true));
其中_bsContatto是ContattoMV的类型,而BsIndirizzo是通过以下方式完成的可绑定属性:
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IndirizzoMV BsIndirizzo
{
get
{
return (IndirizzoMV)_bsIndirizzo.DataSource;
}
set
{
if (value == null)
{
return;
}
_bsIndirizzo.DataSource = value;
}
}
在UcIndirizzo中,属性以这种方式绑定:
this.txtToponimo.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsIndirizzo, "TOPONIMO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
其中_bsIndirizzo是IndirizzoMV的类型。 在UcContatto中,将属性传播到主UserControl,我以这种方式使用另一个可绑定属性:
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ContattoMV BsContatto
{
get
{
return (ContattoMV)_bsContatto.DataSource;
}
set
{
if (value == null)
{
return;
}
_bsContatto.DataSource = value;
}
}
在主控件UcReferenteTecnico中初始化用户控件,我这样做:
this.ucContatto1.BsContatto = new ContattoMV();
当我在txtNome中设置值时更改用户控件中的值时,如果我在txtToponimo中的ucIndirizzo中更改值,则对NOME_CONTATTO属性进行赋值(将断点输入到set属性中)
我的错误在哪里? 非常感谢
我会说您的错误不是使用XAML来定义Binding
,但是我想您可能有一些合理的理由。 由于所有外部类型和属性名称,我发现很难回答您的问题,因此尽管我认为我无法直接帮助您解决问题,但我可以提供以下简单建议:
当您想将数据绑定到父视图模型中的属性时,只需使用RelativeSource Binding
:
想象一下,这是在父视图模型中的:
public string NOME_CONTATTO
{
get { return _NOME_CONTATTO; }
set
{
_NOME_CONTATTO = value;
NotifyPropertyChanged("NOME_CONTATTO");
}
}
您可以像这样从任何子视图将数据直接绑定到它:
<TextBox Text="{Binding DataContext.NOME_CONTATTO,
RelativeSource={RelativeSource AncestorType={x:Type Local:ParentView}}}" ... />
另外,如果您只想在视图模型之间传递一些值,则可以使用delegate
s ...请参见我对如何从其他视图模型调用主视图模型中的函数的回答。 问题,以了解如何做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.