2010-11-10 21 views
5

El tipo se parece a esto:Cómo enlazar datos a datos jerárquicos con un WPF TreeView?

class Category 
{ 
    public string Name; 
    public string Message; 

    public ObservableCollection<Category> SubCategories; 
} 

donde tendrá digamos 5 categorías, donde cada categoría contiene subcategorías entre 0 (nada) a decir 3.

Sé cómo enlazar los datos no jerárquica a un WPF TreeView, pero no puede resolverlo para valores de datos jerárquicos.

Respuesta

7

Aquí es un ejemplo .....

<!-- Create a TreeView, and have it source data from 
     the AnimalCategories collection --> 
    <TreeView ItemsSource="{x:Static local:Window1.AnimalCategories}"> 

    <!-- Specify the template that will display a node 
     from AnimalCategories. I.e., one each for “Amphibians” 
     and “Spiders” in this sample. It will get its nested 
     items from the "Animals" property of each item --> 
    <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate ItemsSource="{Binding Path=Animals}"> 

     <!-- Display the AnimalCategory by showing it's Category string --> 
     <TextBlock FontWeight="Bold" Text="{Binding Path=Category}" /> 

     <!-- Specify the nested template for the individual Animal items 
      that are within the AnimalCategories. E.g. “California Newt”, etc. --> 
     <HierarchicalDataTemplate.ItemTemplate> 
      <DataTemplate> 
      <TextBlock Text="{Binding Path=Name}"/> 
      </DataTemplate> 
     </HierarchicalDataTemplate.ItemTemplate> 

     </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 
    </TreeView> 

este código es de here podría ser más servicial a que lea este artículo, estoy pensando.

+0

Gracias, hombre, voy a intentar esto ahora. –

+0

Realmente lo siento, no pude hacer que esto funcione en xaml, solo funciona en código. Entonces, lo que utilicé es esto: para "{Binding Path = Animals}", utilicé {Binding Path = Categories}. Para {Binding Path = Category} utilicé el mismo, ya que pensé que significaba escribir el nombre del tipo, y para "{Binding Path = Name}", usé el mismo, pensando que este es el miembro que se mostrará. –

2

Primero, querrás convertir todos esos campos en propiedades: el enlace de datos WPF no se puede enlazar a los campos. (Y entonces, la respuesta de Muad'Dib debería funcionar.)

+0

Gracias, sí, fue solo una muestra rápida, no quise codificarla completamente como ejemplo, pero en el código real, son propiedades. –

1

Sé que esta pregunta fue hecha hace mucho tiempo ... pero hay una muy buena example en MSDN que se expande con la respuesta de Muad'Dib.

Su XAML se parece a esto:

<StackPanel x:Name="LayoutRoot" Background="White"> 
     <StackPanel.Resources> 
      <HierarchicalDataTemplate x:Key="ChildTemplate" > 
       <TextBlock FontStyle="Italic" Text="{Binding Path=Title}" /> 
      </HierarchicalDataTemplate> 
      <HierarchicalDataTemplate x:Key="NameTemplate" ItemsSource="{Binding Path=ChildTopics}" ItemTemplate="{StaticResource ChildTemplate}"> 
       <TextBlock Text="{Binding Path=Title}" FontWeight="Bold" /> 
      </HierarchicalDataTemplate> 
     </StackPanel.Resources> 
     <TreeView Width="400" Height="300" ItemsSource="{Binding}" ItemTemplate="{StaticResource NameTemplate}" x:Name="myTreeView" /> 
    </StackPanel> 

me encontré con la combinación de los dos trabajen perfectamente para mí.

Cuestiones relacionadas