wpf TreeView绑定

2016-12-27 12:16:07来源:CSDN作者:qq_31971935人点击

前台:

   <TreeView  BorderThickness="0" Padding="20" x:Name="RegionTree" SelectedItemChanged="RegionTree_SelectedItemChanged">                          </TreeView>    

后台绑定树(二级菜单)

 #region 绑定区域    public string RegionName;    public string UnitName;    public async  void BindRegion()    {        RegionTree.Items.Clear();        //区域        Dictionary<string, string> dic = new Dictionary<string, string>();        dic.Add("tablename", "View_QY");        dic.Add("rolename", "RowGuid,BIMName");        dic.Add("topnumber", " ");        dic.Add("where", " and 1=1");        dic.Add("orderby", " ");        DataTable dtArea = await Backend.ApiCaller.TryGetAsync<DataTable>("api/Common/GetTableInfo", dic);        //房间        Dictionary<string, string> dic1 = new Dictionary<string, string>();        dic1.Add("tablename", "View_FangJian");        dic1.Add("rolename", "RowGuid,BIMName,BIMModelParentGuid");        dic1.Add("topnumber", " ");        dic1.Add("where", " and 1=1");        dic1.Add("orderby", " ");        DataTable dtHouse = await ApiCaller.TryGetAsync<DataTable>("api/Common/GetTableInfo", dic1);        foreach (DataRow dr in dtArea.Rows)        {            TreeViewItem TVI = new TreeViewItem();            TVI.Header =Convert.ToString(dr["BIMName"]);            TVI.Tag =Convert.ToString(dr["RowGuid"]);            RegionName = Convert.ToString(dr["BIMName"]);            TVI.Name = "功能区";            DataRow[] drArr = dtHouse.Select("BIMModelParentGuid='"+ Convert.ToString(dr["RowGuid"]) + "'");            for (int i=0;i<drArr.Length;i++)            {                TreeViewItem TVII = new TreeViewItem();                TVII.Header =Convert.ToString(drArr[i]["BIMName"]);                UnitName = Convert.ToString(drArr[i]["BIMName"]);                TVII.Tag = Convert.ToString(drArr[i]["RowGuid"]);                TVII.Name = "房间";                TVI.Items.Add(TVII);            }            RegionTree.Items.Add(TVI);        }    }    #endregion

选择事件

  #region 树选择        private void RegionTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)        {            TreeViewItem itemQY = this.RegionTree.SelectedItem as TreeViewItem;            if (Convert.ToString(itemQY.Name) != "" && Convert.ToString(itemQY.Name) == "功能区")            {                            QYMC.Text = Convert.ToString(itemQY.Header);                dataBind();            }            if (Convert.ToString(itemQY.Name) != "" && Convert.ToString(itemQY.Name) == "房间")            {                            KJDYMC.Text = Convert.ToString(itemQY.Header);                dataBind();            }        }        #endregion

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台