Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-Windows窗体用户控件,带有子控件和数据绑定_C#_Winforms_Data Binding_User Controls - Fatal编程技术网

C#-Windows窗体用户控件,带有子控件和数据绑定

C#-Windows窗体用户控件,带有子控件和数据绑定,c#,winforms,data-binding,user-controls,C#,Winforms,Data Binding,User Controls,我创建了一个相当简单的用户控件,其中包含一些子控件和组件。我在一个components属性和一个controls属性之间添加了一个数据绑定。到目前为止,当我将用户控件添加到表单时,问题就出现了。VisualStudio序列化程序在InitializeComponent方法中再次添加该绑定,因此引发异常,告诉我该属性已进行数据绑定 另一个小问题是,它还序列化了我在用户控件本身中更改的属性,即使我没有在表单中更改它们。这个问题可能导致上述问题,我真的不知道 用户控制代码: public partia

我创建了一个相当简单的用户控件,其中包含一些子控件和组件。我在一个components属性和一个controls属性之间添加了一个数据绑定。到目前为止,当我将用户控件添加到表单时,问题就出现了。VisualStudio序列化程序在InitializeComponent方法中再次添加该绑定,因此引发异常,告诉我该属性已进行数据绑定

另一个小问题是,它还序列化了我在用户控件本身中更改的属性,即使我没有在表单中更改它们。这个问题可能导致上述问题,我真的不知道

用户控制代码:

public partial class BrowseFolder : UserControl {
    private const string CATEGORY_CONTROLS = "Controls";

    public BrowseFolder() {
        InitializeComponent();
    }

    [Category(CATEGORY_CONTROLS)]
    [Description("The label that describes the folder's meaning in the context it's in.")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Label Label => this.Lbl;
    [Category(CATEGORY_CONTROLS)]
    [Description("The text box that displays or lets the user edit the full path of the folder.")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public TextBox TextBox_DirectoryFullPath => this.TxtBox_DirectoryFullPath;
    [Category(CATEGORY_CONTROLS)]
    [Description("The button that displays the folder browse dialog when clicked.")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Button Button_BrowseDirectory => this.Btn_BrowseDirectory;
    [Category(CATEGORY_CONTROLS)]
    [Description("The folder browse dialog.")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public NotifyPropertyChangedFolderBrowseDialog FolderBrowserDialog => this.Fbd;

    private void Btn_BrowseDirectory_Click(object sender, EventArgs e) =>
        this.FolderBrowserDialog.ShowDialog(this);

    private void InitializeComponent() {
        // initialize...

        // 
        // Btn_BrowseDirectory
        // 
        this.Btn_BrowseDirectory.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.Btn_BrowseDirectory.Location = new System.Drawing.Point(365, 0);
        this.Btn_BrowseDirectory.Margin = new System.Windows.Forms.Padding(3, 0, 0, 0);
        this.Btn_BrowseDirectory.Name = "Btn_BrowseDirectory";
        this.Btn_BrowseDirectory.Size = new System.Drawing.Size(75, 23);
        this.Btn_BrowseDirectory.TabIndex = 9;
        this.Btn_BrowseDirectory.Text = "Browse...";
        this.Btn_BrowseDirectory.UseVisualStyleBackColor = true;
        this.Btn_BrowseDirectory.Click += new System.EventHandler(this.Btn_BrowseDirectory_Click);
        // 
        // Lbl
        // 
        this.Lbl.AutoSize = true;
        this.Lbl.Location = new System.Drawing.Point(-3, 5);
        this.Lbl.Margin = new System.Windows.Forms.Padding(0);
        this.Lbl.Name = "Lbl";
        this.Lbl.Size = new System.Drawing.Size(31, 13);
        this.Lbl.TabIndex = 8;
        this.Lbl.Text = "Text:";
        // 
        // TxtBox_DirectoryFullPath
        // 
        this.TxtBox_DirectoryFullPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.TxtBox_DirectoryFullPath.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.Fbd, "SelectedPath", true));
        this.TxtBox_DirectoryFullPath.Location = new System.Drawing.Point(31, 2);
        this.TxtBox_DirectoryFullPath.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0);
        this.TxtBox_DirectoryFullPath.Name = "TxtBox_DirectoryFullPath";
        this.TxtBox_DirectoryFullPath.Size = new System.Drawing.Size(328, 20);
        this.TxtBox_DirectoryFullPath.TabIndex = 7;

        // initialize...
    }
    // controls fields declerations...
}
表格编号:

class MyForm : Form {
    // other stuff...

    private void InitializeComponent() {
        // initialize...

        // browseFolder1
        // 
        // 
        // browseFolder1.Btn_BrowseDirectory
        // 
        this.browseFolder1.Button_BrowseDirectory.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.browseFolder1.Button_BrowseDirectory.Location = new System.Drawing.Point(365, 0);
        this.browseFolder1.Button_BrowseDirectory.Margin = new System.Windows.Forms.Padding(3, 0, 0, 0);
        this.browseFolder1.Button_BrowseDirectory.Name = "Btn_BrowseDirectory";
        this.browseFolder1.Button_BrowseDirectory.Size = new System.Drawing.Size(75, 23);
        this.browseFolder1.Button_BrowseDirectory.TabIndex = 9;
        this.browseFolder1.Button_BrowseDirectory.Text = "Browse...";
        this.browseFolder1.Button_BrowseDirectory.UseVisualStyleBackColor = true;
        // 
        // browseFolder1.Lbl
        // 
        this.browseFolder1.Label.AutoSize = true;
        this.browseFolder1.Label.Location = new System.Drawing.Point(-3, 5);
        this.browseFolder1.Label.Margin = new System.Windows.Forms.Padding(0);
        this.browseFolder1.Label.Name = "Lbl";
        this.browseFolder1.Label.Size = new System.Drawing.Size(31, 13);
        this.browseFolder1.Label.TabIndex = 8;
        this.browseFolder1.Label.Text = "Text:";
        this.browseFolder1.Location = new System.Drawing.Point(53, 86);
        this.browseFolder1.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
        this.browseFolder1.Name = "browseFolder1";
        this.browseFolder1.Size = new System.Drawing.Size(440, 22);
        this.browseFolder1.TabIndex = 8;
        // 
        // browseFolder1.TxtBox_DirectoryFullPath
        // 
        this.browseFolder1.TextBox_DirectoryFullPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.browseFolder1.TextBox_DirectoryFullPath.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.browseFolder1.FolderBrowserDialog, "SelectedPath", true));
        this.browseFolder1.TextBox_DirectoryFullPath.Location = new System.Drawing.Point(31, 2);
        this.browseFolder1.TextBox_DirectoryFullPath.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0);
        this.browseFolder1.TextBox_DirectoryFullPath.Name = "TxtBox_DirectoryFullPath";
        this.browseFolder1.TextBox_DirectoryFullPath.Size = new System.Drawing.Size(328, 20);
        this.browseFolder1.TextBox_DirectoryFullPath.TabIndex = 7;

        // initialize...
    }
    // controls fields declerations...
} 
正在引发的异常:

您已使用以下内容装饰您的
文本框

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TextBox SomeTextBox { get { return someTextBox; } }
这意味着
SomeTextBox
的所有属性的值与默认值不同,将被序列化

要解决有关
数据绑定的问题,防止将其序列化为表单,请在用户控件中添加数据绑定(如果控件未处于设计模式):

if(LicenseManager.UsageMode != LicenseUsageMode.Designtime)
    someTextBox.DataBindings.Add("Text", SomeOtherControl, "SomeProperty", true);

不了解控件的详细内容,但如果要更改到另一个源的绑定,可以调用
control.DataBindings.Clear()
删除所有绑定,然后添加新的bindingsource。非常感谢,顺便说一句,我很好奇LicenseManager类是什么。不客气。我用它来检测控件是否处于设计模式<代码>设计模式
属性在控件的构造函数中不起作用,所以我使用了。你会发现这很有用。