C# 嵌套用户控件绑定不适用于嵌套属性

C# 嵌套用户控件绑定不适用于嵌套属性,c#,data-binding,C#,Data Binding,我在C#win应用程序中有三个用户控件;主用户称为UCReferenceTecnico,它只包含具有嵌套用户控件UcIndirizzo的UcContatto。 UcContatto有一个名为ContattoMV的模型视图,UcIndirizzo有一个名为INDIRIZOMV的模型视图 UcContatto模型视图具有一个属性和一个嵌套的INDIRIZOMV属性;它们是这样做的: public class ContattoMV:INotifyPropertyChanged { strin

我在C#win应用程序中有三个用户控件;主用户称为UCReferenceTecnico,它只包含具有嵌套用户控件UcIndirizzo的UcContatto。 UcContatto有一个名为ContattoMV的模型视图,UcIndirizzo有一个名为INDIRIZOMV的模型视图

UcContatto模型视图具有一个属性和一个嵌套的INDIRIZOMV属性;它们是这样做的:

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));
        }
    }
}
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IndirizzoMV BsIndirizzo
{
    get
    {
        return (IndirizzoMV)_bsIndirizzo.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsIndirizzo.DataSource = value;
    }
}
this.txtToponimo.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsIndirizzo, "TOPONIMO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ContattoMV BsContatto
{
    get
    {
        return (ContattoMV)_bsContatto.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsContatto.DataSource = value;
    }
}
所有属性都在UcContatto和UcIndirizzo中绑定,以便通过以下方式进行控制: 在UcContatto中:

this.txtNome.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsContatto, "NOME_CONTATTO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
要绑定嵌套用户控件UcIndirizzo,请执行以下操作:

this.ucIndirizzo1.DataBindings.Add(new System.Windows.Forms.Binding("BsIndirizzo", this._bsContatto, "Indirizzo", true));
其中bsContatto为ContattoMV类型,BsIndirizzo为可绑定属性,通过以下方式完成:

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));
        }
    }
}
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IndirizzoMV BsIndirizzo
{
    get
    {
        return (IndirizzoMV)_bsIndirizzo.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsIndirizzo.DataSource = value;
    }
}
this.txtToponimo.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsIndirizzo, "TOPONIMO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ContattoMV BsContatto
{
    get
    {
        return (ContattoMV)_bsContatto.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsContatto.DataSource = value;
    }
}
在UcIndirizzo中,properites以以下方式具有约束力:

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));
        }
    }
}
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IndirizzoMV BsIndirizzo
{
    get
    {
        return (IndirizzoMV)_bsIndirizzo.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsIndirizzo.DataSource = value;
    }
}
this.txtToponimo.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsIndirizzo, "TOPONIMO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ContattoMV BsContatto
{
    get
    {
        return (ContattoMV)_bsContatto.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsContatto.DataSource = value;
    }
}
其中bsIndirizzo是IndirizzoMV的类型。 在UcContatto中,要将属性扩展到主用户控件,我将以以下方式使用另一个可绑定属性:

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));
        }
    }
}
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IndirizzoMV BsIndirizzo
{
    get
    {
        return (IndirizzoMV)_bsIndirizzo.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsIndirizzo.DataSource = value;
    }
}
this.txtToponimo.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsIndirizzo, "TOPONIMO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ContattoMV BsContatto
{
    get
    {
        return (ContattoMV)_bsContatto.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsContatto.DataSource = value;
    }
}
要在主控件UCReferenceTecnico中初始化usercontrol,请执行以下操作:

this.ucContatto1.BsContatto = new ContattoMV();
如果我在txtNome中设置值,则当我更改usercontrol中的值时,NOME_CONTATTO属性将被赋值(在设置属性中输入断点) 如果我在txttobonimo中更改ucIndirizzo中的值,则不会对任何属性进行赋值

我的错误在哪里?
非常感谢

我想说,您的错误是没有使用XAML定义您的
绑定
,但我想您可能有一些合理的理由。由于所有的外来类型和属性名称,我发现很难理解您的问题,因此虽然我认为我无法直接帮助您解决问题,但我可以提供以下简单建议:

如果要将数据绑定到父视图模型中的属性,只需使用
相对资源绑定

假设这是在父视图模型中:

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。。。查看我对问题的回答,了解如何做到这一点