Sto scrivendo l'applicazione di prova in Windows Form. Ha una forma semplice con TextBox e ha bisogno di implementare DataBinding. Ho implementato la classe FormViewModel per contenere i miei dati e ho 1 classe per i miei dati aziendali - TestObject. oggettoL'associazione dati supporta le proprietà nidificate in Windows Form?
Business Data:
public class TestObject : INotifyPropertyChanged
{
private string _testPropertyString;
public string TestPropertyString
{
get
{
return _testPropertyString;
}
set
{
if (_testPropertyString != value)
{
_testPropertyString = value;
RaisePropertyChanged("TestPropertyString");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
ViewModel:
public class FormViewModel : INotifyPropertyChanged
{
private TestObject _currentObject;
public TestObject CurrentObject
{
get { return _currentObject; }
set
{
if (_currentObject != value)
{
_currentObject = value;
RaisePropertyChanged("CurrentObject");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Proprietà:
private FormViewModel _viewModel;
public FormViewModel ViewModel
{
get
{
if (_viewModel == null)
_viewModel = new FormViewModel();
return _viewModel;
}
}
Così ora sto cercando di legare i miei dati a TextBox in questo modo:
TextBox.DataBindings.Add("Text", ViewModel, "CurrentObject.TestPropertyString");
E sorprendentemente, non funziona! Nulla cambia, quando cambio CurrentObject o modifica la proprietà TestPropertyString.
Ma funziona benissimo, quando uso:
TextBox.DataBindings.Add("Text", ViewModel.CurrentObject, "TestPropertyString");
Quindi la mia domanda è: Proprietà non i dati di supporto vincolante annidato?
Grazie per le spiegazioni!
Così non sarà in grado di utilizzarlo in modo tale: "CurrentObject.TestPropertyString"? Qual è l'alternativa al mio codice in WinForms? –
@MaksimGladkov Ho aggiornato la risposta con codice funzionante. Forse non avevi qualcosa di dichiarato di proprietà. – LarsTech
Strano, ma questo non funziona per me. Niente succede comunque. –