简体   繁体   English

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

[英]nested User Control binding not working for nested property

i have three usercontrol in a C# win application ; 我在C#win应用程序中具有三个用户控件; the main User is called UcReferenteTecnico that only contains UcContatto that has a nested usercontrol UcIndirizzo. 主要用户称为UcReferenteTecnico,它仅包含具有嵌套用户控件UcIndirizzo的UcContatto。 UcContatto has a modelView named ContattoMV and UcIndirizzo has a modelview named IndirizzoMV UcContatto有一个名为ContattoMV的模型视图,UcIndirizzo有一个名为IndirizzoMV的模型视图

UcContatto modelview has a properies and a nested IndirizzoMV properties; UcContatto模型视图具有属性和嵌套的IndirizzoMV属性; they are done in this way: 他们是这样完成的:

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));
        }
    }
}

All properties are binding in UcContatto and in UcIndirizzo to Control in this way: In UcContatto: 所有属性在UcContatto和UcIndirizzo中都以这种方式绑定到Control:在UcContatto中:

this.txtNome.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsContatto, "NOME_CONTATTO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

and to bind nested usercontrol UcIndirizzo do this: 并绑定嵌套的usercontrol UcIndirizzo,请执行以下操作:

this.ucIndirizzo1.DataBindings.Add(new System.Windows.Forms.Binding("BsIndirizzo", this._bsContatto, "Indirizzo", true));

where _bsContatto is typeof ContattoMV and BsIndirizzo is bindable properties done in this way: 其中_bsContatto是ContattoMV的类型,而BsIndirizzo是通过以下方式完成的可绑定属性:

[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IndirizzoMV BsIndirizzo
{
    get
    {
        return (IndirizzoMV)_bsIndirizzo.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsIndirizzo.DataSource = value;
    }
}

In UcIndirizzo properites is binding in this way: 在UcIndirizzo中,属性以这种方式绑定:

this.txtToponimo.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsIndirizzo, "TOPONIMO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

where _bsIndirizzo is typeof IndirizzoMV. 其中_bsIndirizzo是IndirizzoMV的类型。 In UcContatto to spread properties to main UserControl i use another bindable properties in this way: 在UcContatto中,将属性传播到主UserControl,我以这种方式使用另一个可绑定属性:

[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ContattoMV BsContatto
{
    get
    {
        return (ContattoMV)_bsContatto.DataSource;
    }
    set
    {
        if (value == null)
        {
            return;
        }
        _bsContatto.DataSource = value;
    }
}

to initialize usercontrol in main Control UcReferenteTecnico i do this: 在主控件UcReferenteTecnico中初始化用户控件,我这样做:

this.ucContatto1.BsContatto = new ContattoMV();

when i change value in my usercontrol if i set value in txtNome , NOME_CONTATTO properties is valued (enter in breakpoint put in set properties) if i change value in ucIndirizzo in txtToponimo no properties is valued 当我在txtNome中设置值时更改用户控件中的值时,如果我在txtToponimo中的ucIndirizzo中更改值,则对NOME_CONTATTO属性进行赋值(将断点输入到set属性中)

where is my error? 我的错误在哪里? thanks a lot 非常感谢

I'd say that your error is not using XAML to define your Binding s , but I guess that you might have some valid reason for that. 我会说您的错误不是使用XAML来定义Binding ,但是我想您可能有一些合理的理由。 I found it really difficult to follow your question because of all of the foreign type and property names, so while I don't think that I can help directly with your problem, I can provide this simple advice: 由于所有外部类型和属性名称,我发现很难回答您的问题,因此尽管我认为我无法直接帮助您解决问题,但我可以提供以下简单建议:

When you want to data bind to a property that is in a parent view model, you can simply use a RelativeSource Binding : 当您想将数据绑定到父视图模型中的属性时,只需使用RelativeSource Binding

Imagine that this was in the parent view model: 想象一下,这是在父视图模型中的:

public string NOME_CONTATTO
{
    get { return _NOME_CONTATTO; }
    set
    {
        _NOME_CONTATTO = value;
        NotifyPropertyChanged("NOME_CONTATTO");
    }
}

You could data bind to it directly from any child view like this: 您可以像这样从任何子视图将数据直接绑定到它:

<TextBox Text="{Binding DataContext.NOME_CONTATTO, 
    RelativeSource={RelativeSource AncestorType={x:Type Local:ParentView}}}" ... />

Alternatively, if you just want to pass some value between view models, you can use delegate s... see my answer to the How to call functions in a main view model from other view models? 另外,如果您只想在视图模型之间传递一些值,则可以使用delegate s ...请参见我对如何从其他视图模型调用主视图模型中的函数的回答 question to find out how to do that. 问题,以了解如何做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM