【问题标题】:Placing Items in a TreeView Side by Side将项目并排放置在 TreeView 中
【发布时间】:2013-05-07 14:55:37
【问题描述】:

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

            }

        }

【问题讨论】:

    标签: c# .net wpf treeview


    【解决方案1】:

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

    具体看看 HierarchicalDataTemplates。有一个very good article here

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

    【讨论】:

    • 为了扩展上述答案,WPF 中的 DataTemplating 可能是最常用的将数据转换为 UI 的方法之一。 ItemsControl(TreeView 的子类)包含您的对象(飞机、足球队、客户等),但它将其显示为一组,例如,圆角框,带有一些文本、一个复选框和一个按钮。我建议阅读 DataTemplating 并进行大量试验以更好地处理它。如果您要编写的 UI 处理的不仅仅是最简单的项目,那么您几乎总是会使用数据模板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    相关资源
    最近更新 更多