1

我有一个自定义控件 OrderEntryCtl,它带有一个 TableLayoutPanel,它在第一行的单元格内使用面板(称为标题行,面板被命名为 HeaderPanel1、HeaderPanel2 等 [总共 8 个面板,第 0 行中的每列 1 个)的 TableLayoutPanel])。首先将每个 Panel 的 BackColor 设置为蓝色。在某些时候,我想将每个 Panel 的 BackColor 更改为灰色。这是我遇到的问题:更改 1 个面板的背景色实际上会更改我添加到表单的所有 OrderEntryCtl 上所有面板的背景色。仅供参考:我不会动态创建任何面板;它们都是通过设计器创建的,VS 创建的代码在 OrderEntryCtl.Designer.cs

所以,考虑一下,我的表单上有一些 OrderEntryCtl(这些是由我的代码在运行时以编程/动态方式添加的,而不是由设计者)。用户与第一个 OrderEntryCtl(称为 OrderEntry1)进行交互,然后单击作为控件一部分的按钮。该按钮的单击事件处理程序需要将 8 个 HeaderPanel 变为灰色:

HeaderPanel1.BackColor = Color.DarkGray;
HeaderPanel2.BackColor = Color.DarkGray;
HeaderPanel3.BackColor = Color.DarkGray;
//etc

我希望 OrderEntry1 的 8 个标题面板变成灰色,但是,所有 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 事件提出建议?? ? 任何人 ?

4

0 回答 0