在树视图中并排放置项目

本文关键字:项目 视图 | 更新日期: 2023-09-27 18:12:42

我试图创建一个TreeView,其中树中的每个元素都包含一个CheckBox和一个ComboBox并排。我能够在TreeView中生成组合框,并尝试使用另一个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);
            }
        }

在树视图中并排放置项目

你需要了解一下WPF中的datatemplate,它们非常强大,会让你的工作轻松很多。

具体来看一下HierarchicalDataTemplates。这里有一篇很好的文章

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