i have three usercontrol in a C# win application ; the main User is called UcReferenteTecnico that only contains UcContatto that has a nested usercontrol UcIndirizzo. UcContatto has a modelView named ContattoMV and UcIndirizzo has a modelview named IndirizzoMV
UcContatto modelview has a properies and a nested IndirizzoMV properties; 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:
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:
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:
[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:
this.txtToponimo.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this._bsIndirizzo, "TOPONIMO", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
where _bsIndirizzo is typeof IndirizzoMV. In UcContatto to spread properties to main UserControl i use another bindable properties in this way:
[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:
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
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. 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
:
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? question to find out how to do that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.