前边研究的是某控件嵌套某控件,这里相当于树嵌套树,但这里不能这么叫,树很特殊,它有一个分层数据模板:HierarchicalDataTemplate,现在来看如何使用,
第一:选构建题目类,再建一个选项类,题目类集合中的每个项包含一个选项类集合,即数组嵌套数组,C#语句如下:
(为方便看清语句,类直接写在主程序中)
C#代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; 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; using System.Collections.ObjectModel;//ObservableCollection命名空间 namespace TreeviewWithHierarchical { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } //题目类 class question { public Int32 questionID//题目ID号 { get; set; } public string questionName//题目名,比如:第1题或第2题等 { get; set; } public ObservableCollection<choseItem> choseItems//某题中"选项类"的集合,比如A-D { get; set; } public question(Int32 _id, string _questionname, ObservableCollection<choseItem> _choseitems)//构造函数 { questionID = _id; questionName = _questionname; choseItems = _choseitems; } } //选项类 class choseItem { public string ChoseName//选项名,比如:A,B,C,D之类 { get; set; } public string ChoseContent//选项内容 { get; set; } } ObservableCollection<question> Questions = new ObservableCollection<question>();//题目数组 private void Window_Loaded(object sender, RoutedEventArgs e) { string[] CharStr = new string[4] { "A", "B", "C", "D" }; for (int i = 0; i < 5; i++) { ObservableCollection<choseItem> ChoseItems = new ObservableCollection<choseItem>();//选项数组 for (int j = 0; j < 4; j++) { choseItem item = new choseItem();//选项类 item.ChoseName = CharStr[j] + ":"; item.ChoseContent = "选项内容举例..."; ChoseItems.Add(item); } Questions.Add(new question(i, "__第" + (i + 1).ToString() + "题", ChoseItems)); } treeview1.ItemsSource = Questions; } } }
第二步:主界面的绑定语句如下:
主界面代码如下:
<Window x:Class="TreeviewWithHierarchical.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TreeviewWithHierarchical" Height="350" Width="525" Loaded="Window_Loaded"> <Window.Resources > <!-- TreeView分层数据模板--> <HierarchicalDataTemplate ItemsSource="{Binding choseItems}" x:Key="myTreeviewWithHierarchical"> <StackPanel Orientation="Horizontal" > <CheckBox Margin="3" IsChecked="False" /> <Image Source="question2.png" Width="24" Height="24" /> <TextBlock Margin="3" Text="{Binding questionName}" /> </StackPanel> <HierarchicalDataTemplate.ItemTemplate > <DataTemplate > <Border Margin="3" BorderBrush="Blue" BorderThickness="1" CornerRadius="3" > <StackPanel Orientation="Horizontal" > <TextBlock Margin="3" Text="选项名:" /> <TextBlock Margin="3" Text="{Binding ChoseName}" /> <TextBlock Margin="3" Text="内容:" /> <TextBlock Margin="3" Text="{Binding ChoseContent}" /> </StackPanel> </Border> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> </HierarchicalDataTemplate> </Window.Resources> <Grid> <TreeView Name="treeview1" Margin="5" ItemTemplate="{StaticResource myTreeviewWithHierarchical}" /> </Grid> </Window>
第三:小结
1:TreeView可以使用分层数据模板HierarchicalDataTemplate,也可以不使用,根据不同的情况来进行选择
比如前边的例子直接操作TreeViewItem即可:
代码说明 Code
<DataTemplate x:Key="myTreeViewGrid"> <TreeViewItem Name="treeviewitem1" > <TreeViewItem.Header > ...... </TreeViewItem.Header> ...... <DataGrid ItemsSource="{Binding choseItems}" > </DataGrid> </TreeViewItem> </DataTemplate>
2:最简单的情形是直接使用TreeView的TreeViewItem作为节点,比如:
代码说明 Code
<TreeView > <TreeViewItem Header="A"> <TreeViewItem Header="a1" /> <TreeViewItem Header="a2" /> </TreeViewItem> <TreeViewItem Header="B"> <TreeViewItem Header="b1" /> <TreeViewItem Header="b2" /> </TreeViewItem> </TreeView>