[英]WinForms. Design-time Data binding to child control's property
I am developing my own "MyTextBox" control 我正在开发自己的“ MyTextBox”控件
public class MyTextBox:UserControl
{
TextBox textBox;
Button button;
public MyTextBox()
{
this.textBox = new TextBox();
this.button = new Button();
this.button.Click += button_Click;
this.Controls.Add(this.textBox);
this.Controls.Add(this.button);
}
public TextBox TextBox
{
get
{
return this.textBox;
}
}
}
Now at Design-time I am putting "MyTextBox" on a form. 现在在设计时,我将“ MyTextBox”放在窗体上。 In the Designer's PropertyGrid I can see that "TextBox" property of myTextBox1 control is visible in a Sub-Grid .
在设计器的PropertyGrid中,我可以看到myTextBox1控件的“ TextBox”属性在Sub-Grid中可见。
I am also adding a "BindingSource" to the form for data binding. 我还在表格中添加了“ BindingSource”以进行数据绑定。
The Sub-Grid is offering "Text" property for binding and I am binding it to the bindingSource1 just as we all usually bind texbox's "Text" property. 子网格提供绑定的“文本”属性,我将其绑定到bindingSource1,就像我们通常都绑定texbox的“文本”属性一样。 I am expecting following code to be generated by the Designer:
我期望设计器生成以下代码:
this.myTextBox1.TextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "FullName"));
The walkthrough above is not giving this effect. 上面的演练没有实现此效果。 I am wondering if there is a way of doing that.
我想知道是否有办法做到这一点。 My intention is to data-bind "Text" property of the TextBox, childed by MyTextBox, at Design-time.
我的意图是在设计时对MyBox子级化的TextBox的“ Text”属性进行数据绑定。
Don't give up too soon. 不要太早放弃。 One of the most useful features of Windows Forms is data-binding.
Windows窗体最有用的功能之一是数据绑定。 For your requirement, first change your
TextBox
property to this: 根据您的要求,首先将
TextBox
属性更改为此:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TextBox TextBox
{
get
{
return textBox1;
}
}
then bind Text
property of your TextBox
of your user control in designer this way: 然后以这种方式在设计器中绑定用户控件的
TextBox
的Text
属性:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.