Viorel
Viorel

Reputation: 61

Binding a property to a control using Windows Forms Designer

I want to bind a property to a control using Windows Forms Designer.

For example, I have this component:

class MyComponent:Component, INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    private string _strProperty;
    [Bindable(true)]
    public string StrProperty {
      get{
        return _strProperty;
      }
      set {
        if (_strProperty != value) {
          _strProperty = value;
          if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("StrProperty"));
        }
      }
    }
  }

I drag this component from the toolbox and drop it on a form. The component name is myComponent1. On the same form I have a TextBox control named textBox1.
Now I want to bind textBox1.Text property to myComponent1.StrProperty property. I know that I can write in code:

textBox1.DataBindings.Add(new Binding("Text", myComponent1, "StrProperty"));

but I want to achieve the same result using the designer. Is it possible? Should I use a BindingSource?

Upvotes: 0

Views: 2510

Answers (2)

GregBlast
GregBlast

Reputation: 41

Since your question is 3 years old I'm not sure this applied to your version of Visual Studio at the time but for others interested now here's how you can do it:

  • select textBox1
  • expand (DataBindings) in the property grid
  • in Text, enter myComponent1 - StrProperty
  • press enter, done

Simple as that. If you check the generated designer code you'll see the new binding created as you would have done it by code.

Upvotes: 3

Herks
Herks

Reputation: 897

No, you can only binding the property using code-behind in WinForm. However, you can binding data in WPF without C# code but using XAML.

Upvotes: 2

Related Questions