usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Windows; usingSystem.Windows.Controls; usingSystem.Windows.Data; usingSystem.Windows.Documents; usingSystem.Windows.Input; usingSystem.Windows.Media; usingSystem.Windows.Media.Imaging; usingSystem.Windows.Navigation; usingSystem.Windows.Shapes; using System.Collections.ObjectModel;//ObservableCollection命名空间 namespaceListBoxInListBox { /// <summary> ///MainWindow.xaml 的交互逻辑 /// </summary> public partial classMainWindow : Window { publicMainWindow() { InitializeComponent(); } //题目类 classquestion { public string qutestionName//题目名,比如:第1题或第2题等 { get; set; } public ObservableCollection<choseItem> choseItems//某题中"选项类"的集合,比如A-D { get; set; } public question(string _questionname, ObservableCollection<choseItem> _choseitems)//构造函数 { qutestionName =_questionname; choseItems =_choseitems; } } //选项类 classchoseItem { public string ChoseName//选项名,比如:A,B,C,D之类 { get; set; } public string ChoseContent//选项内容 { get; set; } } ObservableCollection<question> Questions = new ObservableCollection<question>();//题目数组 private void Window_Loaded(objectsender, 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 + 1).ToString() + "题", ChoseItems)); } listbox1.ItemsSource =Questions; } } }
第二步:主界面的绑定语句如下:
<Window x:Class="ListBoxInListBox.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="ListBox嵌套ListBox测试" Height="350" Width="525" Loaded="Window_Loaded"> <Window.Resources > <!--第一部分:内嵌的ListBox用什么布局--> <ItemsPanelTemplate x:Key="itemspanel"> <StackPanel Orientation="Horizontal" VerticalAlignment="Center" /> </ItemsPanelTemplate> <!-- 第二部分:内嵌ListBox的项模板--> <DataTemplate x:Key="itemstemplate"> <!-- 用Canvas来装可以精确定位项,但没有水平滚动条Canvas.Left="{Binding Path=LeftWidth}" Canvas.Top="0"只好用StackPanel来布局--> <StackPanel Orientation="Vertical" > <Border BorderBrush="Blue" BorderThickness="1" CornerRadius="3" > <StackPanel Orientation="Horizontal" Margin="5,0,5,0"> <!-- 这里是装项内容--> <Image Source="/zero.png" Width="32" Height="32" /> <TextBlock Text="{Binding Path=ChoseName}" VerticalAlignment="Center" Margin="2" /> <TextBox Text="{Binding Path=ChoseContent}" IsReadOnly="True" Margin="2"VerticalAlignment="Center" > </TextBox> </StackPanel> </Border> </StackPanel> </DataTemplate> </Window.Resources> <Grid> <ListBox Name="listbox1" Margin="5" HorizontalContentAlignment="Stretch" ><!-- 最外层ListBox--> <ListBox.ItemTemplate > <DataTemplate > <StackPanel Orientation="Horizontal" > <Border Margin="5" Padding="3" BorderBrush="Green" BorderThickness="1" CornerRadius="2"> <StackPanel Orientation="Horizontal" > <CheckBox Content="选择该题" Margin="3"></CheckBox> <TextBlock Text="{Binding qutestionName}" Margin="3"/> <ListBox Name="listbox2" ItemsSource="{Binding choseItems}"ItemsPanel="{StaticResource itemspanel}"ItemTemplate="{StaticResource itemstemplate}"/> <!-- 如果内嵌项不需要单个项选择,可考虑把ListBox换成ItemsControl--> </StackPanel> </Border> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window>
第三:小结一下: