xaml代码:
<Window x:Class="WpfTest.TreeViewTest"
xmlns="[http://schemas.microsoft.com/winfx/2006/xaml/presentation](http://schemas.microsoft.com/winfx/2006/xaml/presentation)"
xmlns:x="[http://schemas.microsoft.com/winfx/2006/xaml](http://schemas.microsoft.com/winfx/2006/xaml)"
xmlns:src="clr-namespace:WpfTest"
Title="TreeViewTest" Height="300" Width="300" DataContext="{Binding}">
<DockPanel>
<DockPanel.Resources>
<HierarchicalDataTemplate DataType="{x:Type src:NodeNew}" ItemsSource="{Binding Path=Nodes1}">
<StackPanel FlowDirection="RightToLeft" Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Name}"></TextBlock>
<Image Source="Resources/KnowDot.png" Width="15" Height="18"/>
</StackPanel>
</HierarchicalDataTemplate>
</DockPanel.Resources>
<TreeView x:Name="TreeViewtest1"></TreeView>
</DockPanel>
</Window>
后台代码:
namespace WpfTest
{
/// <summary>
/// TreeViewTest.xaml 的交互逻辑
/// </summary>
public partial class TreeViewTest : Window
{
public TreeViewTest()
{
InitializeComponent();
treeViewbinding();
}
/// <summary>
/// 连接数据库
/// </summary>
void treeViewbinding()
{
DataTable dt;
using (SqlConnection conn = new SqlConnection("server=192.168.1.152,2433;database=zhongym;uid=sa;pwd=tianlan"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from TreeViewTest",conn);
dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
transform(dt);
}
/// <summary>
/// 赋值给Node集合
/// </summary>
/// <param name="dt"></param>
public void transform(DataTable dt)
{
List<NodeNew> listNode=new List<NodeNew>();
foreach(DataRow dr in dt.Rows)
{
NodeNew temp=ConvertToTableModel(dr);
//赋值
listNode.Add(temp);
}
List<NodeNew> outputList = Bind(listNode);
TreeViewtest1.ItemsSource = outputList;
}
/// <summary>
/// 赋值给Node
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
public static NodeNew ConvertToTableModel(DataRow dr)
{
NodeNew result = new NodeNew();
if (false == DBNull.Value.Equals(dr["id"].ToString())) result.ID1 = Convert.ToInt32(dr["id"]);
if (false == DBNull.Value.Equals(dr["name"])) result.Name = dr["name"].ToString();
if (false == DBNull.Value.Equals(dr["pid"])) result.ParentID1 = Convert.ToInt32(dr["pid"]);
return result;
}
/// <summary>
/// 绑定树
/// </summary>
List<NodeNew> Bind(List<NodeNew> nodes)
{
//定义一个返回值变量
List<NodeNew> outputList = new List<NodeNew>();
//循环遍历控件
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].ParentID1 == 0) outputList.Add(nodes[i]);//第一次调用时,ParentID为-1 也就是父级
else FindDownward(nodes, nodes[i].ParentID1).Nodes1.Add(nodes[i]);
}
return outputList;
}
/// <summary>
/// 向下查找
/// </summary>
/// <param name="nodes"></param>
/// <param name="id"></param>
/// <returns></returns>
NodeNew FindDownward(List<NodeNew> nodes, int id)
{
if (nodes == null) return null;
for (int i = 0; i < nodes.Count; i++)
{
//寻找子节点
if (nodes[i].ID1 == id)
return nodes[i];
//判断NodeNew是否为空,在这里NodeNew不会为空,所以可不用
NodeNew node = FindDownward(nodes[i].Nodes1, id);
if (node != null) return node;
}
return null;
}
}
/// <summary>
/// 节点类
/// </summary>
public class NodeNew
{
public NodeNew()
{
this.Nodes1 = new List<NodeNew>();
this.ParentID1 = 0;
}
public int ID1 { get; set; }
public string Name { get; set; }
public int ParentID1 { get; set; }
public List<NodeNew> Nodes1 { get; set; }
}
}