我有一个自定义控件OrderEntryCtl,它有一个TableLayoutPanel,它使用第一行单元格中的Panels(称之为标题行,Panels命名为HeaderPanel1,HeaderPanel2等[共8个面板,每列1个)在TableLayoutPanel的第0行中)。每个面板的BackColor首先设置为蓝色。在某些时候,我想将每个Panel的BackColor更改为灰色。这是我遇到的问题:更改1面板的BackColor实际上更改了添加到窗体的所有OrderEntryCtl上所有面板的BackColor。 仅供参考:我没有动态创建任何面板;它们都是通过设计器创建的,VS创建的代码在OrderEntryCtl.Designer.cs中
所以,考虑一下,我的表单上有一些OrderEntryCtl(这些是在我的代码运行时以编程方式/动态添加的,而不是由设计者添加的)。用户与第一个OrderEntryCtl交互(称之为OrderEntry1),然后单击作为控件一部分的按钮。该按钮的click事件处理程序需要将8 HeaderPanel变为灰色:
HeaderPanel1.BackColor = Color.DarkGray;
HeaderPanel2.BackColor = Color.DarkGray;
HeaderPanel3.BackColor = Color.DarkGray;
//etc
我期望只有8个标题面板为OrderEntry1变为灰色,但是,所有OrderEntryCtl的所有标题面板都变为灰色。真奇怪的是,我可以注释掉最后7个“HeaderPanel#.BackColor = Color.DarkGray;”命令,只留下HeaderPanel1.BackColor = Color.DarkGray;这仍然会发生。但是,如果我添加到事件处理程序
HeaderPanel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
只有OrderEntry1的面板2的边框样式受到影响,这是预期的行为! 如何使BackColor属性的行为与我的面板的BorderStyle属性相同?
在回复第一条评论时,我已将代码包含在Designer.cs
中private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Panel HeaderPanel1;
private System.Windows.Forms.Panel HeaderPanel2;
private System.Windows.Forms.Panel HeaderPanel3;
private System.Windows.Forms.Panel HeaderPanel4;
private System.Windows.Forms.Panel HeaderPanel5;
private System.Windows.Forms.Panel HeaderPanel6;
private System.Windows.Forms.Panel HeaderPanel7;
private System.Windows.Forms.Panel HeaderPanel8;
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.HeaderPanel1 = new System.Windows.Forms.Panel();
this.HeaderPanel2 = new System.Windows.Forms.Panel();
this.HeaderPanel3 = new System.Windows.Forms.Panel();
this.HeaderPanel4 = new System.Windows.Forms.Panel();
this.HeaderPanel5 = new System.Windows.Forms.Panel();
this.HeaderPanel6 = new System.Windows.Forms.Panel();
this.HeaderPanel7 = new System.Windows.Forms.Panel();
this.HeaderPanel8 = new System.Windows.Forms.Panel();
this.tableLayoutPanel1.SuspendLayout();
this.HeaderPanel1.SuspendLayout();
this.HeaderPanel2.SuspendLayout();
this.HeaderPanel3.SuspendLayout();
this.HeaderPanel4.SuspendLayout();
this.HeaderPanel5.SuspendLayout();
this.HeaderPanel6.SuspendLayout();
this.HeaderPanel7.SuspendLayout();
this.HeaderPanel8.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.BackColor = System.Drawing.Color.White;
this.tableLayoutPanel1.ColumnCount = 8;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 24F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 26F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 24F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 18F));
this.tableLayoutPanel1.Controls.Add(this.HeaderPanel1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.HeaderPanel2, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.HeaderPanel3, 2, 0);
this.tableLayoutPanel1.Controls.Add(this.HeaderPanel4, 3, 0);
this.tableLayoutPanel1.Controls.Add(this.HeaderPanel5, 4, 0);
this.tableLayoutPanel1.Controls.Add(this.HeaderPanel6, 5, 0);
this.tableLayoutPanel1.Controls.Add(this.HeaderPanel7, 6, 0);
this.tableLayoutPanel1.Controls.Add(this.HeaderPanel8, 7, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(944, 90);
this.tableLayoutPanel1.TabIndex = 0;
//
// HeaderPanel1
//
this.HeaderPanel1.BackColor = global::WdRx.Fulfillment.Dispensing.Workstation.Properties.Settings.Default.OrderEntryHeader;
this.HeaderPanel1.Controls.Add(this.Lbl_Header1);
this.HeaderPanel1.DataBindings.Add(new System.Windows.Forms.Binding("BackColor", global::WdRx.Fulfillment.Dispensing.Workstation.Properties.Settings.Default, "OrderEntryHeader", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.HeaderPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.HeaderPanel1.Location = new System.Drawing.Point(675, 0);
this.HeaderPanel1.Margin = new System.Windows.Forms.Padding(0);
this.HeaderPanel1.Name = "HeaderPanel1";
this.HeaderPanel1.Size = new System.Drawing.Size(107, 45);
this.HeaderPanel1.TabIndex = 10;
好的,在回顾了上述内容之后,我意识到我的问题是我将BackColor属性与我的配置文件中定义的颜色相关联,而我正在为所有面板执行此操作。绑定用于属性更改事件,因此更改1面板上的属性,更新所有面板。
所以,我现在正在讨论如何仍然从我的配置文件中获取我的初始颜色,但没有绑定OnPropertyChange事件? ? ?任何人?