在 wpf 中将 treeviewitem 显示为网格行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24569156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:04:43  来源:igfitidea点击:

Display treeviewitem as grid rows in wpf

c#wpfxamltreeview

提问by makambi

Basically in need to achieve something like this using treeview control in wpf: (random picture)

基本上需要使用 wpf 中的树视图控件来实现这样的东西:(随机图片)


(source: msdn.com)


(来源:msdn.com

Where nodes and child nodes have same headers.

其中节点和子节点具有相同的标头。

I googled a lot, but my knowledge in wpf not that good.

我用谷歌搜索了很多,但我在 wpf 方面的知识并不是那么好。

Here is my parent node class:

这是我的父节点类:

 public class Parent : PropertyChangedBase
    {
        public string ParentName { get; set; }
        public BindableCollection<Child> Children { get; set; }
    }

And child:

还有孩子:

public class Child : PropertyChangedBase
{
    public string ChildName { get; set; }
}

My xaml tree view:

我的 xaml 树视图:

  <TreeView Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Nodes}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type projectModels:Parent}" ItemsSource="{Binding Children}">
                <StackPanel>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <CheckBox Grid.Column="2"></CheckBox>
                        <TextBlock Grid.Column="1" Text="{Binding ParentName}">
                        </TextBlock>
                    </Grid>
                </StackPanel>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type projectModels:Child}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding ChildName}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>

I tried using Gridbut obviously it create different grids, so I can relay on column width.

我尝试使用Grid但显然它创建了不同的网格,所以我可以中继列宽。

I tried How to make gridview a child element of a treeview in wpf application, but they use ListView. It's not an option for me right now, as treeviewitem selection functionality is tightly coupled with my treeview and code behind.

我试过如何使 gridview 成为 wpf 应用程序中树视图的子元素,但他们使用ListView. 这不是我现在的选择,因为 treeviewitem 选择功能与我的 treeview 和代码紧密耦合。

Any ideas how it can be done? Thanks.

任何想法如何做到?谢谢。

回答by Heena Patil

Try this xaml

试试这个 xaml

  <TreeView x:Name="treeviewList"  ItemsSource="{Binding ManufacturerList}">
    <TreeView.ItemTemplate>
        <DataTemplate>
            <TreeViewItem  ItemsSource="{Binding Models}">
                <TreeViewItem.Header>
                    <Grid Width="350">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                            <ColumnDefinition ></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Task}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0"/>
                        <TextBlock Text="{Binding durationTotal}" Margin="10,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center"  Grid.Column="1"/>
                        <TextBlock Text="{Binding HeadNote}" HorizontalAlignment="Left"  VerticalAlignment="Center"  Grid.Column="2"/>
                    </Grid>
                </TreeViewItem.Header>
                <TreeViewItem.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="-20,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100"></ColumnDefinition>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                                <ColumnDefinition></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding SubTask}" Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0"/>
                            <TextBlock Text="{Binding Duration}" Margin="10,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center"  Grid.Column="1"/>
                            <TextBlock Text="{Binding Notes}" HorizontalAlignment="Left" VerticalAlignment="Center"  Grid.Column="2"/>
                        </Grid>
                    </DataTemplate>
                </TreeViewItem.ItemTemplate>
            </TreeViewItem>
        </DataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

c# code

代码

public class Company
{
    public string Task { get; set; }
    public string durationTotal { get; set; }
    public string HeadNote { get; set; }
    public List<Model> Models { get; set; }
}
public class Model
{
    public string SubTask { get; set; }
    public string Duration { get; set; }
    public string Notes { get; set; }      
}


   List<Company> ManufacturerList = new List<Company>();

        ManufacturerList.Add(new Company()
        {
            Task = "Coding",
            durationTotal = "4",
            HeadNote = "Coding Task",
            Models = new List<Model>()
            {new Model(){SubTask = "Write", Duration = "2", Notes ="It pays the bills" },
            new Model(){SubTask = "Compile", Duration = "1", Notes ="c# or go home" },
            new Model(){SubTask = "Test", Duration = "1", Notes ="works on my m/c" },}
        });


        ManufacturerList.Add(new Company()
        {
            Task = "Communicate",
            durationTotal = "2",
            HeadNote = "Communicate Task",
            Models = new List<Model>()
            {new Model(){SubTask = "Email", Duration = "0.5", Notes ="so much junk mail"  },
            new Model(){SubTask = "Blogs", Duration = "0.25", Notes ="blogs.msdn.com/delay" },
            new Model(){SubTask = "Twitter", Duration = "0.25", Notes ="RT:nothing to report" },}
        });

        treeviewList.ItemsSource = ManufacturerList;

Result

结果

enter image description here

在此处输入图片说明

回答by Andrew Stephens

If the only problem with your code is that each treeview item renders with different grid column widths, you can try the "share size scope" feature to align them all. In the TreeView control, set Grid.IsSharedSizeScopeto true:-

如果您的代码唯一的问题是每个树视图项以不同的网格列宽呈现,您可以尝试使用“共享大小范围”功能将它们全部对齐。在 TreeView 控件中,设置Grid.IsSharedSizeScope为 true:-

Then add a SharedSizeGroupto the ColumnDefinitions that should have the same width across all the treeview items (your first column definition has a fixed width anyway, so it's not needed on that):-

然后将 a 添加SharedSizeGroupColumnDefinition所有树视图项中应该具有相同宽度的s (无论如何,您的第一个列定义具有固定宽度,因此不需要):-

<Grid.ColumnDefinitions>
   <ColumnDefinition Width="20" />
   <ColumnDefinition Width="Auto" SharedSizeGroup="A" />
   <ColumnDefinition SharedSizeGroup="B" />
</Grid.ColumnDefinitions>

The values are just strings used to "name" the columns, and can be anything you like.

这些值只是用于“命名”列的字符串,可以是您喜欢的任何内容。