wpf 在 xaml 中带有 TreeViewItems 的 ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14444285/
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
ListView with TreeViewItems in xaml
提问by GP89
I'm pretty new to c#, the first thing that I'm trying to make is a ListViewwith data bindings which has turned out ok.
我对 c# 很陌生,我尝试做的第一件事是ListView使用数据绑定,结果证明没问题。
I'm now trying to make items have a twist button if the underlying model has any children (like the TreeView). Each of the children will have columns the same as all the top level items.
如果底层模型有任何子项(如TreeView),我现在正在尝试使项目具有扭曲按钮。每个子项将具有与所有顶级项相同的列。
How would I go about doing this? Is there an already existing control like this? If not would I be better off dressing up a TreeViewto look like a ListView, or dress up a ListViewto look like a TreeView?
我该怎么做呢?是否已经存在这样的控件?如果不是,我最好打扮TreeView成看起来像一个ListView,还是打扮成一个ListView看起来像一个TreeView?
I went down the road outlined in this solutionwhich dresses up a TreeView, but the end result looks pretty awful and the heading is actually just an item, so you lose all the nice column sizing and column buttons that can hook up to column sorting that you get in ListViewso that route actually seems like it would be more work.
我沿着这个解决方案中概述的道路进行了装饰TreeView,但最终结果看起来非常糟糕,标题实际上只是一个项目,因此您失去了所有可以与您的列排序相关联的漂亮的列大小和列按钮进入ListView这样的路线实际上看起来会更多的工作。
I noticed the new task manager has a control exactly like what I'm trying to create, I don't know how this made? probably in C though.
我注意到新的任务管理器有一个和我想创建的完全一样的控件,我不知道这是怎么做到的?虽然可能在 C 中。


回答by Evil Closet Monkey
Microsoft provides a sample that appears to be what you are looking for. A write-up of the example can be found here:
Microsoft 提供了一个示例,它似乎是您要查找的内容。可以在此处找到该示例的编写:
http://msdn.microsoft.com/en-us/library/vstudio/ms771523(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/vstudio/ms771523(v=vs.90).aspx
When you build and run the example you will end up with something resembling this:
当您构建并运行示例时,您将得到类似于以下内容的结果:


There is a large amount of templating done in the example, so you will be able to make things look the way you want.
示例中完成了大量模板,因此您将能够使事情看起来像您想要的那样。
回答by sa_ddam213
It would probably be easier to Stylethe ListView, I had a quick play and managed to create a mockup in about 10min.
它可能会更容易Style的ListView,我有一个快速播放和管理,创造约10分钟一个样机。


Obviously you will want to add more to it, but it seems pretty easy to host a TreeViewinside the ListViewcolumn.
显然,您会想要向其中添加更多内容,但TreeView在ListView列内托管 a 似乎很容易。
Here is the mockup code if you want to use to build on.
如果您想用于构建,这是模型代码。
Xaml:
Xml:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="233" Width="405" Name="UI">
<Grid DataContext="{Binding ElementName=UI}">
<ListView ItemsSource="{Binding Processes}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Process" Width="200" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TreeView BorderThickness="0" ItemsSource="{Binding Processes}" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Processes}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="CPU" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding CpuUsage, StringFormat={}{0} %}" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Memory" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MemUsage, StringFormat={}{0} MB}" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
</Window>
Code: sorry about the treeible mockup data loading :)
代码:对树状模型数据加载感到抱歉:)
public partial class MainWindow : Window
{
private ObservableCollection<ProcessInfo> _processes = new ObservableCollection<ProcessInfo>();
public MainWindow()
{
InitializeComponent();
Processes.Add(new ProcessInfo
{
CpuUsage = 10.3,
MemUsage = 48.9,
Processes = new ObservableCollection<Process>()
});
var pro = new Process{ Name = "Process1", Processes = new ObservableCollection<Process>()};
pro.Processes.Add(new Process { Name = "SubProcess1", Processes = new ObservableCollection<Process>() });
Processes[0].Processes.Add(pro);
Processes.Add(new ProcessInfo
{
CpuUsage = 0,
MemUsage = 100,
Processes = new ObservableCollection<Process>()
});
var pro2 = new Process { Name = "Process2", Processes = new ObservableCollection<Process>() };
pro2.Processes.Add(new Process { Name = "SubProcess1", Processes = new ObservableCollection<Process>() });
pro2.Processes.Add(new Process { Name = "SubProcess2", Processes = new ObservableCollection<Process>() });
pro2.Processes.Add(new Process { Name = "SubProcess3", Processes = new ObservableCollection<Process>() });
Processes[1].Processes.Add(pro2);
}
public ObservableCollection<ProcessInfo> Processes
{
get { return _processes; }
set { _processes = value; }
}
}
public class ProcessInfo
{
public ObservableCollection<Process> Processes { get; set; }
public double CpuUsage { get; set; }
public double MemUsage { get; set; }
}
public class Process
{
public string Name { get; set; }
public ObservableCollection<Process> Processes { get; set; }
}
Good luck :)
祝你好运 :)
回答by Paul Hoenecke
What you describe sounds a bit like a TreeListView, and if you google 'WPF TreeListView' you will see some solutions that might be good for you. I have used one from Telerik, but it might be overkill depending on how complicated your needs are.
您描述的内容听起来有点像 TreeListView,如果您在 google 上搜索“WPF TreeListView”,您会看到一些可能对您有用的解决方案。我使用了Telerik 的一个,但根据您的需求的复杂程度,它可能有点矫枉过正。
If you only want one sub-level like the image you attached, you might want to just roll your own using a ListView with a complex DataTemplate for the first column which would show an expander button and a simple ListBox bound to the children items.
如果您只想要一个像您附加的图像那样的子级别,您可能只想使用 ListView 滚动您自己的第一列,该列将显示一个扩展按钮和一个绑定到子项的简单 ListBox 的复杂 DataTemplate。
Similar to the answer here, except your cell would have a checkbox styled to look like the arrow, the text for the item, and a child ListBox. Then bind the visibility of the child ListBox to the state of the checkbox.
与此处的答案类似,不同之处在于您的单元格会有一个复选框,其样式类似于箭头、项目文本和子 ListBox。然后将子 ListBox 的可见性绑定到复选框的状态。

