标签:
实体类:
1 using System.Collections.Generic; 2 using System.ComponentModel; 3 4 namespace TreeViewBindingDemo 5 { 6 public class NodeEntry : INotifyPropertyChanged 7 { 8 public NodeEntry() 9 { 10 this.NodeEntrys = new List<NodeEntry>(); 11 this.ParentID = -1; 12 this.IsChecked = true; 13 } 14 int id; 15 public int ID 16 { 17 get { return id; } 18 set { id = value; this.OnPropertyChanged("ID"); } 19 } 20 string name; 21 public string Name 22 { 23 get { return name; } 24 set { name = value; this.OnPropertyChanged("Name"); } 25 } 26 public int ParentID { get; set; } 27 bool isChecked; 28 public bool IsChecked 29 { 30 get { return isChecked; } 31 set { isChecked = value; this.OnPropertyChanged("IsChecked"); } 32 } 33 List<NodeEntry> nodeEntrys; 34 public List<NodeEntry> NodeEntrys 35 { 36 get { return nodeEntrys; } 37 set 38 { 39 nodeEntrys = value; 40 this.OnPropertyChanged("NodeEntrys"); 41 } 42 } 43 public event PropertyChangedEventHandler PropertyChanged; 44 private void OnPropertyChanged(string prop) 45 { 46 if (this.PropertyChanged != null) 47 this.PropertyChanged(this, new PropertyChangedEventArgs(prop)); 48 } 49 } 50 51 }
转换代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace TreeViewBindingDemo 17 { 18 /// <summary> 19 /// MainWindow.xaml 的交互逻辑 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 List<NodeEntry> m_NodeEntrys; 24 List<NodeEntry> m_outputList; 25 public MainWindow() 26 { 27 InitializeComponent(); 28 m_NodeEntrys = new List<NodeEntry>() 29 { 30 new NodeEntry { ID = 2, Name = "北京市", ParentID = 1 }, 31 new NodeEntry { ID = 1, Name = "中国" }, 32 new NodeEntry { ID = 3, Name = "吉林省", ParentID = 1 }, 33 new NodeEntry { ID = 4, Name = "上海市", ParentID = 1 }, 34 new NodeEntry { ID = 5, Name = "海淀区", ParentID = 2 }, 35 new NodeEntry { ID = 6, Name = "朝阳区", ParentID = 2 }, 36 new NodeEntry { ID = 7, Name = "大兴区", ParentID = 2 }, 37 new NodeEntry { ID = 8, Name = "白山市", ParentID = 3 }, 38 new NodeEntry { ID = 9, Name = "长春市", ParentID = 3 }, 39 new NodeEntry { ID = 10, Name = "抚松县", ParentID = 8 }, 40 new NodeEntry { ID = 11, Name = "靖宇县", ParentID = 8 }, 41 new NodeEntry { ID = 13, Name = "靖宇县" }, 42 new NodeEntry { ID = 14, Name = "无名村", ParentID = 13 }, 43 }; 44 m_outputList = Bind(m_NodeEntrys); 45 this.treeView1.ItemsSource = m_outputList; 46 this.treeView2.ItemsSource = m_outputList; 47 } 48 49 private List<NodeEntry> Bind(List<NodeEntry> nodes) 50 { 51 List<NodeEntry> outputList=new List<NodeEntry>(); 52 for (int i = 0; i < nodes.Count; i++) 53 { 54 nodes[i].IsChecked = false; 55 if (nodes[i].ParentID == -1) 56 { 57 outputList.Add(nodes[i]); 58 } 59 else 60 { 61 FindDownward(nodes,nodes[i].ParentID).NodeEntrys.Add(nodes[i]); 62 } 63 } 64 return outputList; 65 } 66 67 private NodeEntry FindDownward(List<NodeEntry> nodes, int id) 68 { 69 if (nodes == null) return null; 70 for (int i = 0; i < nodes.Count; i++) 71 { 72 if (nodes[i].ID == id) 73 { 74 return nodes[i]; 75 } 76 } 77 return null; 78 } 79 80 private void btnOK_Click(object sender, RoutedEventArgs e) 81 { 82 try 83 { 84 m_NodeEntrys.Add(new NodeEntry { ID = 14, IsChecked = true, Name = "法国" }); 85 m_outputList.Add(new NodeEntry { ID = 14, IsChecked = true, Name = "法国" }); 86 //m_outputList = Bind(m_NodeEntrys); 87 NodeEntry node = new NodeEntry(); 88 this.treeView1.ItemsSource = m_outputList; 89 this.treeView2.ItemsSource = null; 90 this.treeView2.ItemsSource = m_outputList; 91 } 92 catch (Exception ex) 93 { 94 } 95 } 96 private void btnCancel_Click(object sender, RoutedEventArgs e) 97 { 98 99 } 100 101 //双向绑定改名,选择 102 private void treeView2_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) 103 { 104 NodeEntry item = (NodeEntry)this.treeView2.SelectedItem; 105 item.Name = "dido"; 106 item.IsChecked = true; 107 MessageBox.Show(item.ID.ToString()); 108 } 109 110 111 } 112 }
标签:
原文地址:http://www.cnblogs.com/zqhxl/p/4491775.html