c# 美化treeview

<Window x:Class="Wpftreeview.MainWindow"
        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;
        }
    }


}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值