并排放置TreeView中的项目

时间:2013-05-07 14:55:37

标签: c# .net wpf treeview

我正在尝试创建一个TreeView,其中树中的每个元素都包含一个CheckBox和一个ComboBox。我能够在TreeView中生成ComboBox并尝试使用另一个TreeView,但这仍然不会产生我想要的输出(我希望CheckBox和ComboBox并排显示,而不是像我一样在另一个之下在下面的代码中管理)有没有办法可以实现这一目标?下面是我现在使用的代码,它非常简单。我只是在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 个答案:

答案 0 :(得分:1)

您需要了解WPF中的DataTemplates,它们非常强大,可以让您的生活更轻松。

具体了解HierarchicalDataTemplates。有一个very good article here

我还发表了一篇关于DataTemplates over on CodeProject的文章,您也会发现它很有用。

相关问题