2012-08-14 20 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