树嵌表格,也算是创新了,展开树时Show出表格,好象不太习惯,WPF就是在这方面比较强大,啥组合都弄得出来,好了操作步骤:
第一:选构建题目类,再建一个选项类,题目类集合中的每个项包含一个选项类集合,即数组嵌套数组,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 DataGridInTree { /// <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_1(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="DataGridInTree.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="DataGridInTreeView" Height="350" Width="525" Loaded="Window_Loaded_1"> <Window.Resources > <DataTemplate x:Key="myTreeViewGrid"> <TreeViewItem Name="treeviewitem1" > <TreeViewItem.Header > <StackPanel Orientation="Horizontal" > <CheckBox IsChecked="False" /> <TextBlock Margin="3" Text="{Binding questionID}"/> <Image Source="png.png" Width="24" Height="24" /> <TextBlock Margin="3" Text="{Binding questionName}" /> </StackPanel> </TreeViewItem.Header> <DataGrid ItemsSource="{Binding choseItems}" AutoGenerateColumns="False" CanUserSortColumns="False" Margin="5" > <DataGrid.Columns > <DataGridTextColumn Header="选项名" Binding="{Binding ChoseName}" IsReadOnly="True" /> <DataGridTextColumn Header="选项内容" Binding="{Binding ChoseContent}" IsReadOnly="True" /> </DataGrid.Columns> </DataGrid> </TreeViewItem> </DataTemplate> </Window.Resources> <Grid> <TreeView Name="treeview1" Margin="5" HorizontalContentAlignment="Stretch" ItemTemplate="{StaticResource myTreeViewGrid}" /> </Grid> </Window>
第三:小结一下:
1:TreeView本身有一个分层数据模板,试了没有弄成,后来一想这里不是分层,而是树嵌套表,应该是两码事,所以直接设置TreeViewItem即可,
2:WPF因为设计思路的新颖性,控件嵌套有点随心所欲,弄出很多怪怪的界面,还真不好适应.
|