11

I am writing the test application in Windows Forms. It has a simple form with TextBox and needs to implement DataBinding. I have implemented the class FormViewModel to hold my data, and have 1 class for my business data — TestObject.

Business Data object:

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

Property:

private FormViewModel _viewModel;
public FormViewModel ViewModel
{ 
    get
    {
        if (_viewModel == null)
            _viewModel = new FormViewModel();

        return _viewModel;
    }
}

So now I'm trying to bind my data to TextBox like this:

TextBox.DataBindings.Add("Text", ViewModel, "CurrentObject.TestPropertyString");

And surprisingly, it doesn't work! Nothing changes, when I change CurrentObject, or change TestPropertyString property.

But it works great, when I use:

TextBox.DataBindings.Add("Text", ViewModel.CurrentObject, "TestPropertyString");

So my question is: Does data binding support nested properties?

Thank you for explanations!

Maksim Gladkov
  • 3,051
  • 1
  • 14
  • 16

1 Answers1

8

The Databinding behavior was changed in .NET 4.0. Your code works on .NET 3.5. I found this issue posted at Microsoft Connect: .Net 4.0 simple binding issue

Here was the work-around that worked for me. Use a BindingSource as the data object:

BindingSource bs = new BindingSource(_viewModel, null);

//textBox1.DataBindings.Add("Text", _viewModel, "CurrentObject.TestPropertyString");
textBox1.DataBindings.Add("Text", bs, "CurrentObject.TestPropertyString");
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • So I will not be able to use it in such way: "CurrentObject.TestPropertyString"? What is the alternative to my code in WinForms? – Maksim Gladkov Jan 17 '12 at 13:46
  • @MaksimGladkov I updated the answer with working code. Maybe you didn't have something declared property. – LarsTech Jan 17 '12 at 14:11
  • Strange, but this doesn't work for me. Nothing happens anyway. – Maksim Gladkov Jan 17 '12 at 15:09
  • @MaksimGladkov I copied your code as is. The only thing I changed was the declaration from `ViewModel" to my `_ViewModel`. You didn't post any code showing how you declared your `ViewModel` and `TestModel` objects — maybe something is amiss there. – LarsTech Jan 17 '12 at 15:15
  • My ViewModel declaration is just "private FormViewModel _viewModel;". Everything is just as yours... What .NET version are you using? – Maksim Gladkov Jan 17 '12 at 15:22
  • 1
    @MaksimGladkov I wasn't aware that Microsoft changed the data binding implementation. The code above works in VS2008 .NET 3.5, but when I tested it on C# 2010 Express .NET 4.0, it didn't work. I get a `Cannot bind to property...` error. Goofy. – LarsTech Jan 17 '12 at 15:33
  • @MaksimGladkov I found the issue over at Microsoft Connect and posted an update. – LarsTech Jan 17 '12 at 15:40