zoukankan      html  css  js  c++  java
  • WPF中TreeView的使用

    因为项目中需要用到TreeView控件,由于是第一次在WPF中用到,因此事先在网上搜了很多关于数据绑定的方法介绍,个人经过实际应用,觉得WPF中的HierarchicalDataTemplate定义模板确实好用很多,但是今天在自己的WPF+MVVM项目中使用了另一种方式。代码不妥之处,望赐教。

        先说数据绑定:

         1、前台Xmal代码:(没有使用模板定义)

           <TreeView Name="treeview"/>  

      /2、在后台的XAML交互逻辑cs代码添加数据上下文并将 treeview作为参数传递到对应的ViewModel中

            public   treeView()
        {
          InitializeComponent();
         this.DataContext = new treeViewVM(this.treeview);
         }

      3、定义实体类,此处举例   

    /// contry: 县城实体类(属性说明自动提取数据库字段的描述信息,对应数据库中的contry表)

    public class contry 
    {
    public contry ();

    string  MC{get;set;}

    int ID{get;set;}

    int parentID{get;set}

    }

       /4、在 treeViewVM中实现数据的绑定    

    //构造函数,接收前台传递过来的treeView对象

    public treeViewVM(TreeView tr)
    {
    this.treeview = tr;
    initialCommand();
    RefreshTreeView(); //刷新treeView,获取数据
    }

    /// <summary>
    /// 创建TreeView的Item
    /// </summary>
    /// <param name=""></param>
    /// <returns></returns>
    private TreeViewItem CreateTreeViewItem(contry cty)
    {
    TreeViewItem tvi = new TreeViewItem();
    tvi.Header = cty.MC;
    tvi.Tag = cty;
    tvi.IsExpanded = true;// 设置数据列表为展开
    return tvi;
    }

    /// <summary>
    /// 获取treeView中的一级节点,并实现递归遍历
    /// </summary>
    /// <param name="contryList"></param>
    /// <returns></returns>
    public List<contry > Bind(List<contry > contryList)
    {

    contryList= DicCacheList.Instance.GetListByTableName<contry >(true);//在缓存的字典中获取contry数据表的list集合,具体封装的方法此处省略;
    var list = contryList.Where(a => a.parentID== null || a.parentID== "_").ToList();//事先在数据库中设置一级节点的parentID(父级ID)为空或者为"_",此处获取一级节点的数据集合;

    if (list.Count == 0)
    {
    return null;
    }
    list.ForEach(a =>
    {
    TreeViewItem tvi1 = CreateTreeViewItem(a);
    treeview.Items.Add(tvi1);
    FindDownward(contryList, a.ID, tvi1);
    });
    return null;
    }

    /// <summary>
    /// 递归遍历treeview的实现方法
    /// </summary>
    /// <param name=""></param>
    /// <param name="id"></param>
    /// <param name="tvi"></param>
    /// <returns></returns>
    public contry FindDownward(List<contry> contryList, string id, TreeViewItem tvi)
    {
    if (contryList == null)
    {
    return null;
    }
    var list = contryList.Where(a => a.parentID== id).ToList();

    if (list.Count == 0)
    {
    return null;
    }
    list.ForEach(a =>
    {
    TreeViewItem tvi2 = CreateTreeViewItem(a);
    tvi.Items.Add(tvi2);
    FindDownward(contryList, a.ID, tvi2);
    });
    return null;
    }

    /// <summary>
    /// 获取treeView列表,刷新列表
    /// </summary>
    private void RefreshTreeView()
    {
    var contryList= DicCacheList.Instance.GetListByTableName<contry>(true);
    treeview.Items.Clear();
    Bind(contryList);
    }

  • 相关阅读:
    由于某些原因无法博客搬家,现在换马甲了 http://blog.csdn.net/qq_32066409
    2017-11-23加深记忆
    (转)poi操作Excel, 各种具体操作和解释
    (转)sqoop常用命令http://www.cnblogs.com/cenyuhai/p/3306037.html
    2017年10月24日制定的3个月的学习目标与计划!!!!!
    (转)Git 提交的正确姿势:Commit message 编写指南
    (转)linux下装tomcat
    简单理解jQuery中$.getJSON、$.get、$.post、$.ajax用法
    程序员必须知道的几个Git代码托管平台(转)
    http://www.blogjava.net 博客园专门针对java的,里面有些大神
  • 原文地址:https://www.cnblogs.com/yingrufeng/p/6067877.html
Copyright © 2011-2022 走看看