用户控件包含组合框,但在设计时设置的项不在运行时 [英] Usercontrol contains a combobox, but the Items set at design time are not in the run time
问题描述
我有一个包含文本框和组合框的用户控件。我已经将combobox的Item属性暴露给用户控件的客户端,因此:
public System.Windows.Forms.ComboBox.ObjectCollection Item
{
get {return baseComboBox.Items; }
}
我将用户控件添加到窗体,并设置Items列表使用表单设计器中的属性值编辑器。然后我运行应用程序,组合框的下拉列表为空。要确认在设计时添加的项目不在列表中,我在客户端表单中添加了以下两行代码:
textBox1.Text = userControl1.Items.Count.ToString();
userControl1.Items.Add(Test item);
当我重新运行应用程序时,测试框显示计数为0(零)
认为用户控件的实例在设计时被引用是一个不同的实例。在运行时被引用,我在设计时设置用户控件的BackColor属性。当我重新运行应用程序时,用户控件的BackColor是我在设计器中设置的。
任何关于为什么项目的设计时间设置的想法
$ b
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ComboBox.ObjectCollection Item {
get {return baseComboBox.Items; }
}
I have a user control that contains a textbox and a combobox. I have exposed the combobox's Item property to clients of the user control, thus:
public System.Windows.Forms.ComboBox.ObjectCollection Item
{
get { return baseComboBox.Items; }
}
I added the user control to a windows form, and set the Items list using the property values editor in the form designer. I then ran the application, and the combobox's drop down list was empty. To confirm that the items added at design time were not in the list, I added the following two lines of code to the client form:
textBox1.Text = userControl1.Items.Count.ToString();
userControl1.Items.Add("Test item");
When I re-ran the application the test box showed a count of 0 (zero), and the drop-down list of the user control contained only "Test item".
Thinking that maybe the instance of the user control being referenced at design time is a different instance from that being referenced at run time, I set the user control's BackColor property at design time. When I re-ran the app, the user control's BackColor was what I had set it to in the designer.
Any ideas on why the design time setting of the Items does not carry over into the run time?
You need an attribute:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ComboBox.ObjectCollection Item {
get { return baseComboBox.Items; }
}
这篇关于用户控件包含组合框,但在设计时设置的项不在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!