TreeView繼承于HeaderedItemsControl類,而HeaderedItemsControl類又繼承于ItemsControl類,所也TreeView同時會擁有ItemsSource屬性設(shè)置數(shù)據(jù)源;DisplayMemberPath設(shè)置數(shù)據(jù)項顯示內(nèi)容;ItemContainerStyle屬性設(shè)置TreeViewItem數(shù)據(jù)項的樣式;ItemContainerStyleSelector屬性設(shè)置TreeViewItem數(shù)據(jù)項樣式選擇器;ItemsPanel屬性設(shè)置數(shù)據(jù)項的容器;ItemsPanel屬性設(shè)置數(shù)據(jù)項容器的選擇器;GroupStyle屬性顯示數(shù)據(jù)分組;GroupStyleSelector屬性設(shè)置數(shù)據(jù)分組選擇器。
與ListBoxItem不同的是每一個TreeViewItem都繼承于HeaderedItemsControl類所以TreeViewItem是一個列表型的容器(而ListBoxItem是內(nèi)容型的容器)。
在聲明TreeViewItem的數(shù)據(jù)模板時也與ListBoxItem有所不同,它使用
關(guān)于TreeView綁定數(shù)據(jù),及數(shù)據(jù)模板的使用的示例代碼如下所示:
由于在樹中顯示數(shù)據(jù),所以使用了兩個嵌套的類型作為實例類,兩個實體類的代碼如下:
{
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>
本文來自2sws的博客,原文地址:http://hi.baidu.com/wangjunwangjuna/blog/item/0cb1de16977f7509962b4320.html