2009-07-12 45 views
0

我有一个可观察集合的数组,我想在一个列表框中显示所有这些集合。这些集合中的每个集合中的数据都是相同类型的,并且它们是根据特定类别分开的。Silverlight中的列表框的DataTemplate可以是列表的集合

所以我的问题是,列表框的DataTemplate是否可能包含Listbox?

回答

1

是,作为一个例子,XAML中:

<UserControl x:Class="SilverlightApplication1.Page" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300"> 
    <Grid x:Name="LayoutRoot" Background="White"> 
     <ListBox ItemsSource="{Binding }"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel > 
         <TextBlock Text="{Binding Name}" /> 
         <ListBox ItemsSource="{Binding InnerList}"> 
          <TextBlock Text="{Binding }" /> 
         </ListBox> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</UserControl> 

的代码:

using System.Collections.Generic; 
using System.Windows.Controls; 

namespace SilverlightApplication1 
{ 
    public partial class Page : UserControl 
    { 
     public Page() 
     { 
      InitializeComponent(); 
      this.DataContext = new List<Data>() 
      { 
       new Data(){Name = "First"}, 
       new Data(){Name = "Second"}, 
       new Data(){Name = "Third"}, 
       new Data(){Name = "FourthWithDifferentData", InnerList=new List<string>(){"a", "b", "c"}} 
      }; 
     } 
    } 
} 

public class Data 
{ 
    public List<string> InnerList { get; set; } 
    public string Name { get; set; } 
    public Data() 
    { 
     InnerList = new List<string>(){"String1", "String2", "String3"}; 
    } 
} 
相关问题