xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:mode="clr-namespace:Wpftreeview"
Height="350" Width="525"
WindowStartupLocation="CenterScreen"
>
<Grid>
<Grid.Resources>
<!--文件夹-->
<HierarchicalDataTemplate DataType="{x:Type mode:NodeBase}" ItemsSource="{Binding ChildNodes}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<Image Source="/Wpftreeview;Component/Resources/folder.png" Width="16" Height="16" />
<TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}"/>
</StackPanel>
</HierarchicalDataTemplate>
<!--根-->
<HierarchicalDataTemplate DataType="{x:Type mode:FolderNode}" ItemsSource="{Binding ChildNodes}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<Image Source="/Wpftreeview;Component/Resources/root.png" Width="16" Height="16" />
<TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}"/>
</StackPanel>
</HierarchicalDataTemplate>
</Grid.Resources>
<TreeView Name="TreeView" >
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Wpftreeview
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<NodeBase> nodes = new List<NodeBase>()
{
new NodeBase { ID = 1, Name = "中国" },
new NodeBase { ID = 2, Name = "北京市", ParentID = 1 },
new NodeBase { ID = 3, Name = "吉林省", ParentID = 1 },
new NodeBase { ID = 4, Name = "上海市", ParentID = 1 },
new NodeBase { ID = 5, Name = "海淀区", ParentID = 2 },
new NodeBase { ID = 6, Name = "朝阳区", ParentID = 2 },
new NodeBase { ID = 7, Name = "大兴区", ParentID = 2 },
new NodeBase { ID = 8, Name = "白山市", ParentID = 3 },
new NodeBase { ID = 9, Name = "长春市", ParentID = 3 },
new NodeBase { ID = 10, Name = "抚松县", ParentID = 8 },
new NodeBase { ID = 11, Name = "靖宇县", ParentID = 8 }
};
// 绑定树
List<NodeBase> outputList = Bind(nodes);
this.TreeView.ItemsSource = outputList;
FolderNode folder = new FolderNode();
outputList[0].ChildNodes.Add(folder);
outputList[0].IsExpanded = true;
}
/// <summary>
/// 绑定树
/// </summary>
List<NodeBase> Bind(List<NodeBase> nodes)
{
List<NodeBase> outputList = new List<NodeBase>();
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].ParentID == -1)
{
outputList.Add(nodes[i]);
}
else
{
//方法1,用Linq比较给力
NodeBase node = nodes.FirstOrDefault(n => n.ID == nodes[i].ParentID);
if (node != null)
{
node.ChildNodes.Add(nodes[i]);
}
//方法2
//FindDownward(nodes, nodes[i].ParentID).
// ChildNodes.Add(nodes[i]);
}
}
return outputList;
}
/// <summary>
/// 递归查找等于当前id的节点
/// </summary>
/// <param name="nodes"></param>
/// <param name="id"></param>
/// <returns></returns>
NodeBase FindDownward(List<NodeBase> nodes, int id)
{
if (nodes == null) return null;
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].ID == id)
{
return nodes[i];
}
NodeBase node = FindDownward(nodes[i].ChildNodes, id);
if (node != null)
{
return node;
}
}
return null;
}
}
public class FolderNode : NodeBase { }
public class RootNode : NodeBase { }
public class NodeBase
{
public NodeBase()
{
this.ChildNodes = new List<NodeBase>();
this.ParentID = -1;
}
public int ID { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
public List<NodeBase> ChildNodes { get; set; }
//前台进行双向绑定
public bool IsSelected
{
get;
set;
}
//前台进行双向绑定
public bool IsExpanded
{
get;
set;
}
}
}