2009-07-12 4 views
0

J'ai un tableau de collections observables et je veux afficher toutes ces collections dans une seule zone de liste. Les données de chacune de ces collections sont du même type et ont été séparées en fonction d'une catégorie particulière. Donc, ma question est la suivante: est-il possible que le DataTemplate d'une listbox contienne une Listbox?Le DataTemplate d'une listbox dans silverlight peut être une collection de listes

Répondre

1

Oui, à titre d'exemple, le 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> 

Le code:

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"}; 
    } 
} 
Questions connexes