XAML.CS代码:
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 }; 43 m_outputList = Bind(m_NodeEntrys); 44 this.treeView1.ItemsSource = m_outputList; 45 this.treeView2.ItemsSource = m_outputList; 46 } 47 48 private List<NodeEntry> Bind(List<NodeEntry> nodes) 49 { 50 List<NodeEntry> outputList=new List<NodeEntry>(); 51 for (int i = 0; i < nodes.Count; i++) 52 { 53 nodes[i].IsChecked = false; 54 if (nodes[i].ParentID == -1) 55 { 56 outputList.Add(nodes[i]); 57 } 58 else 59 { 60 FindDownward(nodes,nodes[i].ParentID).NodeEntrys.Add(nodes[i]); 61 } 62 } 63 return outputList; 64 } 65 66 private NodeEntry FindDownward(List<NodeEntry> nodes, int id) 67 { 68 if (nodes == null) return null; 69 for (int i = 0; i < nodes.Count; i++) 70 { 71 if (nodes[i].ID == id) 72 { 73 return nodes[i]; 74 } 75 } 76 return null; 77 } 78 79 private void btnOK_Click(object sender, RoutedEventArgs e) 80 { 81 try 82 { 83 m_NodeEntrys.Add(new NodeEntry { ID = 14, IsChecked = true, Name = "法国" }); 84 m_outputList.Add(new NodeEntry { ID = 14, IsChecked = true, Name = "法国" }); 85 //m_outputList = Bind(m_NodeEntrys); 86 NodeEntry node = new NodeEntry(); 87 this.treeView1.ItemsSource = m_outputList; 88 this.treeView2.ItemsSource = null; 89 this.treeView2.ItemsSource = m_outputList; 90 } 91 catch (Exception ex) 92 { 93 } 94 } 95 private void btnCancel_Click(object sender, RoutedEventArgs e) 96 { 97 98 } 99 100 //双向绑定改名,选择 101 private void treeView2_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) 102 { 103 NodeEntry item = (NodeEntry)this.treeView2.SelectedItem; 104 item.Name = "dido"; 105 item.IsChecked = true; 106 MessageBox.Show(item.ID.ToString()); 107 } 108 109 110 } 111 }
实体类:
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 }
XAML代码:
1 <Window x:Class="TreeViewBindingDemo.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:model="clr-namespace:TreeViewBindingDemo" 5 Title="MainWindow" Height="350" Width="525"> 6 <Grid> 7 <Grid.Resources> 8 <HierarchicalDataTemplate DataType="{x:Type model:NodeEntry}" ItemsSource="{Binding NodeEntrys}"> 9 <StackPanel Orientation="Horizontal" Margin="0,2,0,2"> 10 <CheckBox Focusable="False" 11 VerticalAlignment="Center" IsChecked="{Binding IsChecked, Mode=TwoWay}"/> 12 <TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding ID}"/> 13 </StackPanel> 14 <HierarchicalDataTemplate.ItemContainerStyle> 15 <Style TargetType="TreeViewItem"> 16 <Setter Property="IsExpanded" Value="True" /> 17 </Style> 18 </HierarchicalDataTemplate.ItemContainerStyle> 19 </HierarchicalDataTemplate> 20 </Grid.Resources> 21 <Grid.ColumnDefinitions> 22 <ColumnDefinition MinWidth="100" Width="170*"/> 23 <ColumnDefinition Width="1*" /> 24 <ColumnDefinition Width="80" /> 25 <ColumnDefinition Width="80" /> 26 </Grid.ColumnDefinitions> 27 <Grid.RowDefinitions> 28 <RowDefinition Height="40*" /> 29 <RowDefinition Height="40" /> 30 </Grid.RowDefinitions> 31 <GridSplitter Grid.Column="1" Grid.RowSpan="5" Name="gridSplitter1" HorizontalAlignment="Stretch" /> 32 <Button Content="确定" Grid.Column="2" Grid.Row="1" Height="25" HorizontalAlignment="right" Margin="0,10,0,0" Name="btnOK" Click="btnOK_Click" VerticalAlignment="Top" Width="50" IsDefault="True" /> 33 <Button Content="取消" Grid.Column="3" Grid.Row="1" Height="25" HorizontalAlignment="right" Margin="0,10,10,0" Name="btnCancel" Click="btnCancel_Click" VerticalAlignment="Top" Width="50" IsCancel="True" /> 34 <TreeView Grid.ColumnSpan="2" Name="treeView1" > 35 </TreeView> 36 <TreeView Grid.Column="2" Grid.ColumnSpan="2" Name="treeView2" SelectedItemChanged="treeView2_SelectedItemChanged"> 37 </TreeView> 38 </Grid> 39 40 </Window>
时间: 2024-10-05 04:55:35