简体   繁体   English

通过父用户控件从子控件传递数据绑定

[英]Pass Databinding From Child Control Through Parent UserControl

I'm trying to build a custom usercontrol (winforms) with a richtextbox and would like to pass the "Text" databinding through the usercontrol so that when the control is placed on a form and a bindingsource is bound to the usercontrol's "Text" databinding, it will actually bind to the child richtextbox "Text" binding. 我正在尝试使用RichTextBox构建自定义用户控件(winforms),并希望通过用户控件传递“文本”数据绑定,以便在控件放置在窗体上并将绑定源绑定到用户控件的“文本”数据绑定时,它实际上将绑定到子Richtextbox的“文本”绑定。 The code bit below works for setting the text but won't get the text after it has been changed. 下面的代码位可用于设置文本,但更改后将无法获取文本。

User Control: 用户控制:

[System.ComponentModel.DefaultBindingProperty("Text")]
    public partial class UcEditableRTBox : UserControl
    {
        public UcEditableRTBox()
        {
            InitializeComponent();
            InitControl();
        }

        [Bindable(true)]
        public override string Text
        {
            get { return rtb.Text; }
            set { rtb.Text = value; }
        }

Form: 形成:

public partial class FrmTest : Form
    {
        public FrmTest()
        {
            InitializeComponent();
        }

        TestTable tt;

        private void FrmTest_Load(object sender, EventArgs e)
        {
            tt = TestTable.FindAll().FirstOrDefault();
            bindingSource.DataSource = tt;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bindingSource.EndEdit();
            tt.Save();
        }
    }

Any hint's as to how to go about this or additional resources to look in to would be very much appreciated. 非常感谢有关如何进行此操作或查看其他资源的任何提示。

Thanks 谢谢

The standard fix is to use a different name other than Text : 标准解决方案是使用不同于Text的其他名称:

public string TextValue {
  get { return rtb.Text; }
  set { rtb.Text = value; }
}

See Data Binding with a Custom UserControl's Text Property for other workaround ideas. 有关其他变通办法,请参阅使用自定义UserControl的Text属性进行数据绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM