C# 如何使用SplitContainer将数据绑定到自定义Windows窗体?

C# 如何使用SplitContainer将数据绑定到自定义Windows窗体?,c#,winforms,data-binding,C#,Winforms,Data Binding,我创建了一个称为ListChooser的表单。看起来是这样的: 它由System.Windows.Forms.Button和.TextBox组成 点击按钮(如上图所示,文本为“水果”)会弹出一个对话框,进行一些选择并填充文本框,如下所示: 我的问题是我不知道如何将System.Windows.Forms.Binding设置到此文本框。我认为复杂的是,它不仅仅是一个文本框,它是一个组件中的两个组件。(我知道如何用一个独立的文本框绑定数据)。如果转到文本框>属性>数据绑定>高级>…,则属性下列出

我创建了一个称为ListChooser的表单。看起来是这样的:
它由System.Windows.Forms.Button和.TextBox组成

点击按钮(如上图所示,文本为“水果”)会弹出一个对话框,进行一些选择并填充文本框,如下所示:
我的问题是我不知道如何将System.Windows.Forms.Binding设置到此文本框。我认为复杂的是,它不仅仅是一个文本框,它是一个组件中的两个组件。(我知道如何用一个独立的文本框绑定数据)。如果转到文本框>属性>数据绑定>高级>…,则属性下列出的所有条目都不可选择:

我试过这个,但没有效果:

private System.Windows.Forms.BindingSource myBindingSource;
this.myBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.myBindingSource.DataSource = typeof(Foods) // Fruits is a Property of Foods;
myListChooser.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myBindingSource, "Fruits", true));
我该怎么做

ListChooser.Designer.cs:

partial class ListChooser: UserControl
{
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.btnPopup = new System.Windows.Forms.Button();
        this.textBox = new System.Windows.Forms.TextBox();
        this.splitContainer1 = new System.Windows.Forms.SplitContainer();
        this.splitContainer1.Panel1.SuspendLayout();
        this.splitContainer1.Panel2.SuspendLayout();
        this.splitContainer1.SuspendLayout();
        this.SuspendLayout();
        // 
        // btnPopup
        // 
        this.btnPopup.AutoSize = true;
        this.btnPopup.Dock = System.Windows.Forms.DockStyle.Fill;
        this.btnPopup.Location = new System.Drawing.Point(0, 0);
        this.btnPopup.Margin = new System.Windows.Forms.Padding(0);
        this.btnPopup.Name = "btnPopup";
        this.btnPopup.Size = new System.Drawing.Size(75, 21);
        this.btnPopup.TabIndex = 0;
        this.btnPopup.Text = "Type";
        this.btnPopup.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        this.btnPopup.UseVisualStyleBackColor = true;
        this.btnPopup.Click += new System.EventHandler(this.btnPopup_Click);
        // 
        // txtList
        // 
        this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textBox.BackColor = System.Drawing.SystemColors.Window;
        this.textBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.textBox.Location = new System.Drawing.Point(0, 0);
        this.textBox.Name = "txtList";
        this.textBox.ReadOnly = true;
        this.textBox.Size = new System.Drawing.Size(297, 20);
        this.textBox.TabIndex = 1;
        this.textBox.TabStop = false;
        this.textBox.TextChanged += new System.EventHandler(this.txtList_TextChanged);
        //this.textBox.Leave += new System.EventHandler(this.txtList_Leave);
        // 
        // splitContainer1
        // 
        this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
        this.splitContainer1.Location = new System.Drawing.Point(0, 0);
        this.splitContainer1.Margin = new System.Windows.Forms.Padding(0);
        this.splitContainer1.Name = "splitContainer1";
        // 
        // splitContainer1.Panel1
        // 
        this.splitContainer1.Panel1.Controls.Add(this.btnPopup);
        // 
        // splitContainer1.Panel2
        // 
        this.splitContainer1.Panel2.Controls.Add(this.textBox);
        this.splitContainer1.Size = new System.Drawing.Size(375, 21);
        this.splitContainer1.SplitterDistance = 75;
        this.splitContainer1.SplitterWidth = 3;
        this.splitContainer1.TabIndex = 2;
        // 
        // ListChooser
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.WhiteSmoke;
        this.Controls.Add(this.splitContainer1);
        this.MaximumSize = new System.Drawing.Size(1200, 21);
        this.Name = "ListChooser";
        this.Size = new System.Drawing.Size(375, 21);
        this.splitContainer1.Panel1.ResumeLayout(false);
        this.splitContainer1.Panel1.PerformLayout();
        this.splitContainer1.Panel2.ResumeLayout(false);
        this.splitContainer1.Panel2.PerformLayout();
        this.splitContainer1.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button btnPopup;
    private System.Windows.Forms.TextBoxBase textBox;

    private SplitContainer splitContainer1;
}
部分类ListChooser:UserControl
{
///  
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
///  
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域组件设计器生成的代码
///  
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.btnPopup=new System.Windows.Forms.Button();
this.textBox=new System.Windows.Forms.textBox();
this.splitContainer1=new System.Windows.Forms.SplitContainer();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
这个.SuspendLayout();
// 
//btnPopup
// 
this.btnPopup.AutoSize=true;
this.btnPopup.Dock=System.Windows.Forms.DockStyle.Fill;
this.btnPopup.Location=新系统图点(0,0);
this.btnPopup.Margin=new System.Windows.Forms.Padding(0);
this.btnPopup.Name=“btnPopup”;
this.btnPopup.Size=新系统图纸尺寸(75,21);
this.btnPopup.TabIndex=0;
this.btnPopup.Text=“Type”;
this.btnPopup.TextAlign=System.Drawing.ContentAlignment.middleft;
this.btnPopup.UseVisualStyleBackColor=true;
this.btnPopup.Click+=新建System.EventHandler(this.btnPopup\u Click);
// 
//txtList
// 
this.textBox.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|System.Windows.Forms.AnchorStyles.Right);
this.textBox.BackColor=System.Drawing.SystemColors.Window;
this.textBox.Font=new System.Drawing.Font(“Microsoft Sans Serif”,8.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(0)));
this.textBox.Location=新系统.Drawing.Point(0,0);
this.textBox.Name=“txtList”;
this.textBox.ReadOnly=true;
this.textBox.Size=新系统.Drawing.Size(297,20);
this.textBox.TabIndex=1;
this.textBox.TabStop=false;
this.textBox.TextChanged+=new System.EventHandler(this.txtList\u TextChanged);
//this.textBox.Leave+=new System.EventHandler(this.txtList\u Leave);
// 
//拆分容器1
// 
this.splitContainer1.Dock=System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel=System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.Location=新系统图点(0,0);
this.splitContainer1.Margin=new System.Windows.Forms.Padding(0);
this.splitContainer1.Name=“splitContainer1”;
// 
//splitContainer1.Panel1
// 
this.splitContainer1.Panel1.Controls.Add(this.btnPopup);
// 
//拆分容器1.2
// 
this.splitContainer1.Panel2.Controls.Add(this.textBox);
this.splitContainer1.Size=新系统图纸尺寸(375,21);
this.splitContainer1.SplitterDistance=75;
this.splitContainer1.SplitterWidth=3;
this.splitContainer1.TabIndex=2;
// 
//列表选择器
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.BackColor=System.Drawing.Color.whitemoke;
this.Controls.Add(this.splitContainer1);
此最大尺寸=新系统图纸尺寸(1200,21);
this.Name=“ListChooser”;
该尺寸=新系统图纸尺寸(375,21);
此.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
此.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
this.splitContainer1.ResumeLayout(false);
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.Button btnPopup;
private System.Windows.Forms.TextBoxBase textBox;
专用SplitContainerSplitContainer1;
}

如果您不是太远,您可能需要考虑使用和应用程序的设计模式。WPF被设计成可以很好地处理这样的绑定。如果你想用一个使用这种模式的表单提出一个答案,我愿意接受。你不能绑定到一个私有成员。我想,我不认为我可以给出一个很好的例子,而不需要更多地了解你在应用程序中所做的事情。即使是应用程序的屏幕截图也可能是helpful@HansPassant当前位置正在考虑你的建议。我发现一个问题,我的自定义控件
listcooser
继承自
System.Windows.Forms.UserControl
,它没有