wpf WPF中的多列树视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/463915/
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
Multi-column Tree-View in WPF
提问by Micah
Anyone know where I can get a control like the Multi-column Tree-View in WPF?
任何人都知道我可以在哪里获得像 WPF 中的多列树视图这样的控件?
采纳答案by MrSlippers
回答by Paolo Moretti
SharpDevelophas ListView
subclass called SharpTreeView
which does what you are looking for.
SharpDevelop有一个ListView
名为的子类SharpTreeView
,它可以满足您的需求。
You can see a live example of this control in SharpDevelop's "Watch" window:
您可以在 SharpDevelop 的“观察”窗口中看到此控件的实时示例:
XAML used in the Watch window (as in 5.1.0 beta):
监视窗口中使用的 XAML(如 5.1.0 测试版):
<tv:SharpGridView x:Key="variableGridView" AllowsColumnReorder="False">
<GridView.Columns>
<GridViewColumn Header="{core:Localize MainWindow.Windows.Debug.LocalVariables.NameColumn}" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<tv:SharpTreeNodeView />
<local:AutoCompleteTextBox x:Name="name" Margin="-6 0 0 0" MinWidth="100" Text="{Binding Node.Name}" IsEditable="{Binding Node.CanSetName}">
<local:AutoCompleteTextBox.ContextMenu>
<MultiBinding Converter="{StaticResource menuBuilder}">
<Binding Path="Node.ContextMenuAddInTreeEntry" />
<Binding Path="Node" />
</MultiBinding>
</local:AutoCompleteTextBox.ContextMenu>
</local:AutoCompleteTextBox>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{core:Localize MainWindow.Windows.Debug.LocalVariables.ValueColumn}" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<local:AutoCompleteTextBox
MinWidth="100"
Text="{Binding Node.Value}"
IsEditable="{Binding Node.CanSetValue}">
<local:AutoCompleteTextBox.ContextMenu>
<MultiBinding Converter="{StaticResource menuBuilder}">
<Binding Path="Node.ContextMenuAddInTreeEntry" />
<Binding Path="Node" />
</MultiBinding>
</local:AutoCompleteTextBox.ContextMenu>
</local:AutoCompleteTextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{core:Localize MainWindow.Windows.Debug.LocalVariables.TypeColumn}" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<local:AutoCompleteTextBox MinWidth="100" Text="{Binding Node.Type}" IsEditable="False">
<local:AutoCompleteTextBox.ContextMenu>
<MultiBinding Converter="{StaticResource menuBuilder}">
<Binding Path="Node.ContextMenuAddInTreeEntry" />
<Binding Path="Node" />
</MultiBinding>
</local:AutoCompleteTextBox.ContextMenu>
</local:AutoCompleteTextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</tv:SharpGridView>
The resource is set to the View property of a SharpTreeView control.
该资源设置为 SharpTreeView 控件的 View 属性。
回答by PatrickV
If you don't need column headers, this is fairly easy. You can just provide an item container template, add fixed width grid columns to the right, and bind it to your item's relevant data.
如果您不需要列标题,这很容易。您可以只提供一个项目容器模板,在右侧添加固定宽度的网格列,并将其绑定到您项目的相关数据。
The default item container for a TreeView has a grid defined as (with some added comments):
TreeView 的默认项容器有一个定义为的网格(添加了一些注释):
<Grid>
<Grid.ColumnDefinitions>
<!--Expander--><ColumnDefinition MinWidth="19" Width="Auto"/>
<!--Item--><ColumnDefinition Width="Auto"/>
<!--Overflow--><ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<!--Current Item--><RowDefinition Height="Auto"/>
<!--Sub-items--><RowDefinition/>
</Grid.RowDefinitions>
<ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
</Grid>
So just extend this grid to contain predictable-width columns to the right and you will have columns.
因此,只需扩展此网格以包含右侧的可预测宽度的列,您就会拥有列。
- Add column definition for new column, make it auto-sized
- Update the column span of "ItemsHost" to span the added column
- Add a predictable-width control to that column:
- 为新列添加列定义,使其自动调整大小
- 更新“ItemsHost”的列跨度以跨越添加的列
- 向该列添加一个可预测宽度的控件:
Portions affected:
受影响的部分:
<Grid>
...
<ColumnDefinition Width="Auto"/>
...
<ItemsPresenter ... Grid.ColumnSpan="3" ... />
...
<Border Grid.Column="3"><!--Add column data here--></Border>
...
</Grid>
If the added border is the same width for all rows, you will have a view that feels like a treeview with columns. This obviously will not scale well, but if just need a quick and dirty solution, you should be able to do this in blend in a few minutes without adding dependencies for external controls / libraries.
如果添加的所有行的边框宽度相同,您将拥有一个感觉像带有列的树视图的视图。这显然不能很好地扩展,但如果只需要一个快速而肮脏的解决方案,您应该能够在几分钟内完成此操作,而无需添加外部控件/库的依赖项。
Our needs quickly grew and we added a grid with column labels outside the tree and databound the width of columns there to the widths of these "columns" [actually the widths of a spanning control inthe columns in question using auto-sized columns]. It works, which is all I will say in its defense.
我们的需求迅速增长,我们添加了列标签网格树和数据绑定外列的宽度有这些“列” [一个跨越控制的实际宽度的宽度,在使用自动大小的列有问题的列。它有效,这就是我要为它辩护的全部内容。
This approach could also be done in the generated item template instead of the item container if you don't mind (or want) the selection box (the "Bd" border) extending across all of the columns.
如果您不介意(或不想要)选择框(“Bd”边框)延伸到所有列,也可以在生成的项目模板而不是项目容器中完成此方法。
回答by Patrick Klug
There is an article on codeproject that might help:
有一篇关于 codeproject 的文章可能会有所帮助:
http://www.codeproject.com/KB/WPF/TreeListView.aspx
http://www.codeproject.com/KB/WPF/TreeListView.aspx
There are quite a few other useful articlesthere about the Wpf TreeView.
还有很多其他关于 Wpf TreeView 的有用文章。