2013-05-07 29 views
0

我想創建一個TreeView,其中樹中的每個元素並排包含一個CheckBox和一個ComboBox。我能夠在TreeView中生成組合框,並嘗試使用另一個TreeView,但這仍然不會生成我想要的輸出(我希望CheckBox和ComboBox並排顯示,而不是像其他像下面那樣的一個)在下面的代碼中進行管理)有沒有一種方法可以實現這一目標?下面是我現在使用的代碼,它非常簡單。我只是將項目添加到TreeView中,是否有方法可以將兩個項目並排放置在TreeView中?並排放置TreeView中的項目

TreeViewItem foo = new TreeViewItem(); 
     foo.Header = groupName.Text; 
     treeView1.Items.Add(foo); 
     Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); 
     dlg.Filter = "Text files (*.txt)|*.txt"; 
     dlg.FilterIndex = 0; 
     dlg.Multiselect = true; 
     dlg.RestoreDirectory = true; 
     dlg.Title = "Read .txt Log File"; 
     if (dlg.ShowDialog() == true) 
     { 
      newList = new DynoFileList(groupName.Text); 
      foreach (String file in dlg.FileNames) 
      { 
       TreeViewItem foo2 = new TreeViewItem(); 
       DynoFile testing = new DynoFile(file,groupName.Text); // Creating a new Dyno run file 
       CheckBox test = new CheckBox(); 
       ComboBox ColorSelect = new ComboBox(); 
       test.Click += new RoutedEventHandler(BoxClicked); 
       test.Content = testing.getName(); 
       foo.Items.Add(test); 
       foo.Items.Add(foo2); // This does not quite produce the output that I desire, I was the items to be side by side, not cascaded in any way. The second TreeView is not absolutely necessary 
       foo2.Items.Add(ColorSelect);// This does not quite produce the output I want 
       newList.addRun(testing); 
       allBoxes.Add(test); 

      } 

     } 

回答

1

你需要看看到WPF的DataTemplates,他們是seriosly強大,會讓你的生活變得更加簡單。

具體看看HierarchicalDataTemplates。有一個very good article here

我也發表了一篇關於DataTemplates over on CodeProject的文章,你也會發現有用的。

+1

爲了擴展上述答案,WPF中的DataTemplating如果可能是最經常使用的將數據轉換爲UI的方法之一。一個ItemsControl包含你的對象(飛機,足球隊,顧客等等),但是它將它顯示爲一組圓形框,包括一些文本,一個複選框和一個按鈕。我會建議閱讀DataTemplating,並嘗試使用它,以獲得良好的處理。如果您正在編寫一個將處理的內容比最簡單的項目更多的UI,那麼您幾乎總是會使用數據模板。 – CodeWarrior 2013-05-07 16:15:46