XAML.CS代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace TreeViewBindingDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
List<NodeEntry> m_NodeEntrys;
List<NodeEntry> m_outputList;
public MainWindow()
{
InitializeComponent();
m_NodeEntrys = new List<NodeEntry>()
{
new NodeEntry { ID = , Name = "北京市", ParentID = },
new NodeEntry { ID = , Name = "中国" },
new NodeEntry { ID = , Name = "吉林省", ParentID = },
new NodeEntry { ID = , Name = "上海市", ParentID = },
new NodeEntry { ID = , Name = "海淀区", ParentID = },
new NodeEntry { ID = , Name = "朝阳区", ParentID = },
new NodeEntry { ID = , Name = "大兴区", ParentID = },
new NodeEntry { ID = , Name = "白山市", ParentID = },
new NodeEntry { ID = , Name = "长春市", ParentID = },
new NodeEntry { ID = , Name = "抚松县", ParentID = },
new NodeEntry { ID = , Name = "靖宇县", ParentID = },
new NodeEntry { ID = , Name = "靖宇县" }
};
m_outputList = Bind(m_NodeEntrys);
this.treeView1.ItemsSource = m_outputList;
this.treeView2.ItemsSource = m_outputList;
} private List<NodeEntry> Bind(List<NodeEntry> nodes)
{
List<NodeEntry> outputList=new List<NodeEntry>();
for (int i = ; i < nodes.Count; i++)
{
nodes[i].IsChecked = false;
if (nodes[i].ParentID == -)
{
outputList.Add(nodes[i]);
}
else
{
FindDownward(nodes,nodes[i].ParentID).NodeEntrys.Add(nodes[i]);
}
}
return outputList;
} private NodeEntry FindDownward(List<NodeEntry> nodes, int id)
{
if (nodes == null) return null;
for (int i = ; i < nodes.Count; i++)
{
if (nodes[i].ID == id)
{
return nodes[i];
}
}
return null;
} private void btnOK_Click(object sender, RoutedEventArgs e)
{
try
{
m_NodeEntrys.Add(new NodeEntry { ID = , IsChecked = true, Name = "法国" });
m_outputList.Add(new NodeEntry { ID = , IsChecked = true, Name = "法国" });
//m_outputList = Bind(m_NodeEntrys);
NodeEntry node = new NodeEntry();
this.treeView1.ItemsSource = m_outputList;
this.treeView2.ItemsSource = null;
this.treeView2.ItemsSource = m_outputList;
}
catch (Exception ex)
{
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{ } //双向绑定改名,选择
private void treeView2_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
NodeEntry item = (NodeEntry)this.treeView2.SelectedItem;
item.Name = "dido";
item.IsChecked = true;
MessageBox.Show(item.ID.ToString());
} }
}
实体类:
using System.Collections.Generic;
using System.ComponentModel; namespace TreeViewBindingDemo
{
public class NodeEntry : INotifyPropertyChanged
{
public NodeEntry()
{
this.NodeEntrys = new List<NodeEntry>();
this.ParentID = -;
this.IsChecked = true;
}
int id;
public int ID
{
get { return id; }
set { id = value; this.OnPropertyChanged("ID"); }
}
string name;
public string Name
{
get { return name; }
set { name = value; this.OnPropertyChanged("Name"); }
}
public int ParentID { get; set; }
bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set { isChecked = value; this.OnPropertyChanged("IsChecked"); }
}
List<NodeEntry> nodeEntrys;
public List<NodeEntry> NodeEntrys
{
get { return nodeEntrys; }
set
{
nodeEntrys = value;
this.OnPropertyChanged("NodeEntrys");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
} }
XAML代码:
<Window x:Class="TreeViewBindingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:model="clr-namespace:TreeViewBindingDemo"
Title="MainWindow" Height="" Width="">
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type model:NodeEntry}" ItemsSource="{Binding NodeEntrys}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<CheckBox Focusable="False"
VerticalAlignment="Center" IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
<TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding ID}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="" Width="170*"/>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="" />
<ColumnDefinition Width="" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40*" />
<RowDefinition Height="" />
</Grid.RowDefinitions>
<GridSplitter Grid.Column="" Grid.RowSpan="" Name="gridSplitter1" HorizontalAlignment="Stretch" />
<Button Content="确定" Grid.Column="" Grid.Row="" Height="" HorizontalAlignment="right" Margin="0,10,0,0" Name="btnOK" Click="btnOK_Click" VerticalAlignment="Top" Width="" IsDefault="True" />
<Button Content="取消" Grid.Column="" Grid.Row="" Height="" HorizontalAlignment="right" Margin="0,10,10,0" Name="btnCancel" Click="btnCancel_Click" VerticalAlignment="Top" Width="" IsCancel="True" />
<TreeView Grid.ColumnSpan="" Name="treeView1" >
</TreeView>
<TreeView Grid.Column="" Grid.ColumnSpan="" Name="treeView2" SelectedItemChanged="treeView2_SelectedItemChanged">
</TreeView>
</Grid> </Window>