2012-08-14 109 views
0

我正在尝试为多个对象创建一些类型的控件列表。该对象用于显示在屏幕上的模式,并具有多个不同的属性,例如持续时间,偏移量,下一个模式,模式索引。我希望能够创建一个可迭代的控件列表,以便我可以在GUI中轻松设置每个模式的不同属性。这是我正在创建的表单的草稿。这里是我创建的表单的链接。对象列表控件

enter image description here

每一行为模式的属性的集合。我希望能够以某种方式遍历代码中的每一行,并根据用户输入设置每个模式的属性。从我读到目前为止,我想我想使用一个ItemsControl工具,并以某种方式将它绑定到GUI,但我不知道如何做到这一点,或者如果这是我应该做的。我现在使用的是带有多个面板的TableLayoutPanel,但似乎并没有太多的控制这些工具。我应该如何将每条线的控件分组在一起,然后有效地遍历每条线?

回答

0

好吧我认为我已经实现了为体育运动所做的一切,我比WPF更了解WPF,但这里是我的解决方案,我只是“自我教导”自己。

我打算假设您的解决方案中不会有任何问题。

首先创建一个用户控件。我在WinForm项目上>右键单击>添加>用户控件。这是您要添加组成行的内容的地方。因此,它应该是这样的,我叫我的用户控制RowContent:

Control

确保你有你的名字你的控件。因此,对于复选框,我将其命名为stprIndex_chkBox,enable_chkBox等。

现在您需要为每个控件实现所需的功能。所以,你要改变你的stprIndex_chkBox.Textenable_chkBox.Checked的价值。您还想要访问您的numericalUpDowns的Value。所以在RowContent.cs中,我添加了getters和setters,这取决于我从表单中需要的数据。因此,这里的存取的片段(请记住,你将要添加更多)

public partial class RowContent : UserControl 
{ 
    public RowContent() 
    { 
     InitializeComponent(); 
    } 

    public string SetChkBox1Text 
    { 
     set { stprIndex_chkBox.Text = value; } 
    } 

    public bool IsEnabledChecked 
    { 
     get { return enable_chkBox.Checked; } 
    } 
} 

现在你看,这些都会让你的RowContent类的外部变量的访问。我们来看看TablePanelLayout控件。

我创建了一个额外的用户控件,就像我创建RowContent的方式一样,但是这次我将它命名为ContentCollection。我将User Control的AutoSize设置为true,并将TableLayoutPanel(名为tableLayoutPanel1)放到它上面。

为了节省时间起见,我添加了所有的控制进行动态如下:

public partial class ContentCollection : UserControl 
{ 
    public ContentCollection() 
    { 
     InitializeComponent(); 
     RowContent one = new RowContent(); 
     RowContent two = new RowContent(); 
     RowContent three = new RowContent(); 
     RowContent four = new RowContent(); 
     RowContent five = new RowContent(); 
     RowContent six = new RowContent(); 
     tableLayoutPanel1.Controls.Add(one); 
     tableLayoutPanel1.Controls.Add(two); 
     tableLayoutPanel1.Controls.Add(three); 
     tableLayoutPanel1.Controls.Add(four); 
     tableLayoutPanel1.Controls.Add(five); 
     tableLayoutPanel1.Controls.Add(six); 
     tableLayoutPanel1.SetRow(one, 0); 
     tableLayoutPanel1.SetRow(two, 1); 
     tableLayoutPanel1.SetRow(three, 2); 
     tableLayoutPanel1.SetRow(four, 3); 
     tableLayoutPanel1.SetRow(five, 4); 
     tableLayoutPanel1.SetRow(six, 5); 
    } 
} 

这给了我:

enter image description here

现在,在这里你可以看到我们动态添加这些东西。我希望您可以在WinForm中使用它时了解如何“定制”此用户控件。同样在这一文件中,您会希望添加更多的getter/setter方法/取决于你想做的事,就像其他的用户控制哪些功能;所以,AddAdditionalRow(RowContext rc)等等。我居然被骗,改变了tableLayoutPanelpublic使这个快到底。

所以最后,你有你的ContentCollection,将保留您的自定义对象,现在你需要将它添加到您的窗体。

转到您的形式,打开你的工具箱,并滚动到顶部,看看你的表格有!将它拖放到主窗体和vio'la上。现在,要遍历RowContent,它很容易。因为我拖放弃我的用户控件到窗体上,我能够说出它(userControl12),并开始访问控制的时候了。看看我如何添加复选标记到所有其他复选框,并动态地改变步进指数:

public partial class Form1 : Form 
{ 
    static int i = 0; 
    public Form1() 
    { 
     InitializeComponent(); 
     foreach (RowContent ctrl in userControl11.tableLayoutPanel1.Controls) 
     { 
      ctrl.SetChkBox1Text = i.ToString(); 
      if (!ctrl.IsEnabledChecked && i % 2 == 0) 
       ctrl.IsEnabledChecked = true; 
      i++; 
     } 
     foreach (RowContent ctrl in userControl12.tableLayoutPanel1.Controls) 
     { 
      ctrl.SetChkBox1Text = i.ToString(); 
      i++; 
     } 
    } 
} 

enter image description here

我不是说这是最好的设计有...因为它不是,但它说明了如何做这样的事情。如果您有任何问题,请告诉我。

+0

这是一个好主意。我做了类似的事情。也许我会做出一些改变,比如让你更有效率。谢谢! – Sportikus 2012-08-16 17:00:29