TreeView繼承于HeaderedItemsControl類(lèi),而HeaderedItemsControl類(lèi)又繼承于ItemsControl類(lèi),所也TreeView同時(shí)會(huì )擁有ItemsSource屬性設置數據源;DisplayMemberPath設置數據項顯示內容;ItemContainerStyle屬性設置TreeViewItem數據項的樣式;ItemContainerStyleSelector屬性設置TreeViewItem數據項樣式選擇器;ItemsPanel屬性設置數據項的容器;ItemsPanel屬性設置數據項容器的選擇器;GroupStyle屬性顯示數據分組;GroupStyleSelector屬性設置數據分組選擇器。
與ListBoxItem不同的是每一個(gè)TreeViewItem都繼承于HeaderedItemsControl類(lèi)所以TreeViewItem是一個(gè)列表型的容器(而ListBoxItem是內容型的容器)。
在聲明TreeViewItem的數據模板時(shí)也與ListBoxItem有所不同,它使用
關(guān)于TreeView綁定數據,及數據模板的使用的示例代碼如下所示:
由于在樹(shù)中顯示數據,所以使用了兩個(gè)嵌套的類(lèi)型作為實(shí)例類(lèi),兩個(gè)實(shí)體類(lèi)的代碼如下:
{
private string classname;
private ObservableCollection<Stu> students;
public string ClassName
{
get { return classname; }
set
{
classname = value;
OnPropertyChanged(new PropertyChangedEventArgs("ClassName"));
}
}
public ObservableCollection<Stu> Students
{
get { return students; }
set
{
students = value;
OnPropertyChanged(new PropertyChangedEventArgs("Students"));
}
}
#region INotifyPropertyChanged 成員
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged!=null)
{
PropertyChanged(this, e);
}
}
#endregion
}
class Stu:INotifyPropertyChanged
{
private int myid;
private string mynames;
private int myage;
private string mysex;
private string myaddress;
public int id
{
get { return myid; }
set
{
myid = value;
OnPropertyChanged(new PropertyChangedEventArgs("id"));
}
}
public string names
{
get { return mynames; }
set
{
mynames = value;
OnPropertyChanged(new PropertyChangedEventArgs("names"));
}
}
public int age
{
get { return myage; }
set
{
myage = value;
OnPropertyChanged(new PropertyChangedEventArgs("age"));
}
}
public string sex
{
get { return mysex; }
set
{
mysex = value;
OnPropertyChanged(new PropertyChangedEventArgs("sex"));
}
}
public string address
{
get { return myaddress; }
set
{
myaddress = value;
OnPropertyChanged(new PropertyChangedEventArgs("address"));
}
}
#region INotifyPropertyChanged 成員
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
#endregion
}
資源字典的XAML代碼如下所示:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPF中的TreeView控件.MyDictionary"
xmlns:local="clr-namespace:WPF中的TreeView控件">
<ObjectDataProvider x:Key="MyDataBase"
ObjectType="{x:Type local:wangjundatabase}"
MethodName="GetAllStu"
IsAsynchronous="True">
</ObjectDataProvider>
<HierarchicalDataTemplate x:Key="treeviewtemplate"ItemsSource="{Binding Path=Students}">
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="abc"/>
</Grid.ColumnDefinitions>
<Border Margin="2"
CornerRadius="3"
Background="DarkGoldenrod">
<StackPanel>
<TextBlock Text="{Binding Path=ClassName}" Margin="5"></TextBlock>
</StackPanel>
</Border>
</Grid>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate>
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="abcd"/>
</Grid.ColumnDefinitions>
<Border Margin="2"
CornerRadius="3"
Background="Chocolate">
<StackPanel>
<TextBlock Text="{Binding Path=id}" Margin="5,1,5,1"></TextBlock>
<TextBlock Text="{Binding Path=names}" Margin="5,1,5,1"></TextBlock>
<TextBlock Text="{Binding Path=age}" Margin="5,1,5,1"></TextBlock>
<TextBlock Text="{Binding Path=sex}" Margin="5,1,5,1"></TextBlock>
<TextBlock Text="{Binding Path=address}" Margin="5,1,5,1"></TextBlock>
</StackPanel>
</Border>
</Grid>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</ResourceDictionary>
窗體XAML代碼如下所示:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="600" WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyDictionary/MyDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<TreeView Margin="10" ItemTemplate="{StaticResource treeviewtemplate}"
ItemsSource="{Binding Source={StaticResource MyDataBase}}"
SnapsToDevicePixels="True" Grid.IsSharedSizeScope="True">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="Padding" Value="0"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</Grid>
</Window>
本文來(lái)自2sws的博客,原文地址:http://hi.baidu.com/wangjunwangjuna/blog/item/0cb1de16977f7509962b4320.html

