1.xaml
xmlns:local="clr-namespace:YaoMinSys.Class"
2.class
namespace YaoMinSys.Class{ class PropertyNodeItem { public int ID { get; set; } public string DisplayName { get; set; } public ListChildren { get; set; } public PropertyNodeItem() { Children = new List (); } }}
3.xaml.cs
private void Page_Loaded(object sender, RoutedEventArgs e) { ShowTreeView(); } private void ShowTreeView() { Listitemlist = new List (); try { YaoMinSys.BLL.ZiDianLei ZDLBll = new BLL.ZiDianLei(); DataSet ZDLDs = ZDLBll.GetList(""); for (int i = 0; i < ZDLDs.Tables[0].Rows.Count; i++) { PropertyNodeItem Parent = new PropertyNodeItem { ID = int.Parse(ZDLDs.Tables[0].Rows[i]["ZDLID"].ToString()), DisplayName = ZDLDs.Tables[0].Rows[i]["ZDLeiMing"].ToString(), }; YaoMinSys.BLL.ZiDianXiang ZDXBll = new BLL.ZiDianXiang(); DataSet ZDXDs = ZDXBll.GetList(" ZDLID = " + ZDLDs.Tables[0].Rows[i]["ZDLID"]); for (int j = 0; j < ZDXDs.Tables[0].Rows.Count; j++) { PropertyNodeItem children = new PropertyNodeItem { ID = int.Parse(ZDXDs.Tables[0].Rows[j]["ZDXID"].ToString()), DisplayName = ZDXDs.Tables[0].Rows[j]["ZDXiang"].ToString() }; Parent.Children.Add(children); } itemlist.Add(Parent); } this.Treeview.ItemsSource = itemlist; } catch (Exception) { throw; } } private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { TextBlock tb = (TextBlock)sender; MessageBox.Show("Text :"+tb.Text+"ID :"+tb.Tag); }