2012-01-17 19 views
9

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!

risposta

8

Il comportamento Databinding è stato modificato in .NET 4.0. Il tuo codice funziona su .NET 3.5. Ho trovato questo problema pubblicato in Microsoft Connect: .Net 4.0 simple binding issue

Ecco il work-around che ha funzionato per me. Utilizzare un BindingSource come l'oggetto di dati:

BindingSource bs = new BindingSource(_viewModel, null); 

//textBox1.DataBindings.Add("Text", _viewModel, "CurrentObject.TestPropertyString"); 
textBox1.DataBindings.Add("Text", bs, "CurrentObject.TestPropertyString"); 
+0

Così non sarà in grado di utilizzarlo in modo tale: "CurrentObject.TestPropertyString"? Qual è l'alternativa al mio codice in WinForms? –

+0

@MaksimGladkov Ho aggiornato la risposta con codice funzionante. Forse non avevi qualcosa di dichiarato di proprietà. – LarsTech

+0

Strano, ma questo non funziona per me. Niente succede comunque. –

Problemi correlati