2013-02-08 70 views
0

我想要使用Windows窗体设计器将属性绑定到控件。使用Windows窗体设计器将属性绑定到控件

例如,我有这样的组成:

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

我从工具箱拖曳此组件拖放在窗体上。组件名称是myComponent1。在同一个表单上我有一个名为textBox1的TextBox控件。
现在我想绑定textBox1.Text属性到myComponent1.StrProperty属性。 我知道我可以在代码中写:

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

,但我想用设计来达到同样的效果。可能吗?我应该使用BindingSource吗?

回答

2

不,您只能在WinForm中使用代码隐藏绑定属性。但是,您可以在不使用C#代码的WPF中绑定数据,但使用XAML。

1

由于您的问题是,3岁的我不知道这个应用到你的Visual Studio版本的时间,但对别人感兴趣,现在这里是你如何能做到这一点:

  • 选择textBox1的
  • 扩大(数据绑定)在属性网格
  • 文本,进入myComponent1 - StrProperty
  • 进入,做

这么简单。如果您检查生成的设计器代码,您将看到按照您通过代码完成的创建的新绑定。

相关问题