wpf 树视图绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5295644/
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
wpf treeview binding
提问by pojo
Is there any simple tutorial for beginners about treeview binding in WPF?
有没有关于WPF 中树视图绑定的初学者简单教程?
What should we write in ItemsSource, DataType, ItemTemplate attributes if there's one List of items?
ItemsSource, DataType, ItemTemplate 属性有一个List of items应该怎么写?
IList<string> items = new List<string>();
items.Add("item1");
items.Add("item2");
items.Add("item3");
XAML code:
XAML 代码:
<TreeView Name="treeView1">
<TreeView.Resources> <!-- what does it mean? -->
<HierarchicalDataTemplate DataType="???" ItemsSource="{Binding ???}"></HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
回答by Sherlock
To Fully understand how to use the wpf treeview with data binding, I went through the following tutorials in order -
为了完全理解如何使用 wpf 树视图和数据绑定,我按顺序浏览了以下教程 -
1) A very simple example of treeview binding using recursion
1)一个非常简单的使用递归的treeview绑定的例子
http://testdrivendevelopment.wordpress.com/2008/07/15/databinding-wpf-treeview-using-recursion/
http://testdrivendevelopment.wordpress.com/2008/07/15/databinding-wpf-treeview-using-recursion/
2) Claus Konrads simple example of data binding with the treeview. It's the most straightforward example I have come across and should get any newcomers to wpf up to speed.
2) Claus Konrads 与树视图数据绑定的简单示例。这是我遇到的最直接的例子,应该让 wpf 的任何新手都跟上进度。
http://blog.clauskonrad.net/2011/04/how-to-make-hierarchical-treeview.html
http://blog.clauskonrad.net/2011/04/how-to-make-hierarchical-treeview.html
3) Mike Hillbergs tutorial shows, in detail, the ins and outs of the treeview, how it compares to other wpf controls, and how to bind data.
3) Mike Hillbergs 教程详细展示了树状视图的来龙去脉,它与其他 wpf 控件的比较,以及如何绑定数据。
回答by geometrikal
The trick is that ItemsSource
points to the next collection down.
诀窍是ItemsSource
指向下一个集合。
e.g Imagine you have a collection of type A, and each A contains a description and a collection of type B; and each B contains a description and a collection of type C. The binding would look like this:
例如,假设你有一个类型 A 的集合,每个 A 包含一个描述和一个类型 B 的集合;每个 B 包含一个描述和一个 C 类型的集合。绑定看起来像这样:
<TreeView Width="400" ItemsSource="{Binding CollectionOfA}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type TypeA}" ItemsSource="{Binding CollectionOfB}">
<TreeViewItem Header="{Binding TypeADescription}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type TypeB}" ItemsSource="{Binding CollectionOfC}">
<TreeViewItem Header="{Binding TypeBDescription" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type TypeC}">
<TreeViewItem Header="{Binding TypeC}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
回答by biju
Treeview is one control in wpf that you have to appoach in a little diffrent manner.It is simple and efficient and at the same time a pain to understand and get in track for a beginer,especially those coming from the windows appliaction backgroud.Please go through the MVVMpattern first and then try to approach the treeview.
Treeview 是 wpf 中的一个控件,您必须以稍微不同的方式进行访问。它简单高效,同时对于初学者来说理解和跟踪很痛苦,尤其是那些来自 Windows 应用程序背景的控件。请去首先通过MVVM模式,然后尝试接近树视图。
The Josh Smith article below is a good place to start.
下面的 Josh Smith 文章是一个很好的起点。
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx