views:

264

answers:

1

I'm trying to display a class using a treeview but nothing is displayed. I've inspected the container with Snoop and I see the StackPanel, but it's empty, even the the project does have a title and pages. There are no binding errors and I have no idea why it's not working.

public class Project : INotifyPropertyChanged {
        public string Title {get;set;}
        public ObservableCollection<InfoPage> Pages {get;set;}
    ...

public class InfoPage : INotifyPropertyChanged {
        public string Title {get;set;}
        public ObservableCollection<InfoPage> Pages {get;set;}
    ...

<HierarchicalDataTemplate x:Key="ProjectPageTemplate" ItemsSource="{Binding Pages}">
    <TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="ProjectTemplate" ItemsSource="{Binding Pages}">
    <StackPanel>
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
    </StackPanel>
    <HierarchicalDataTemplate.ItemTemplate>
        <HierarchicalDataTemplate>
            <TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
        </HierarchicalDataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>

Then on the Window itself:

public static DependencyProperty ProjectProperty = DependencyProperty.Register("Project", typeof(Project), typeof(WinMain));
public Project Project {
    get { return base.GetValue(ProjectProperty) as Project; }
    set { base.SetValue(ProjectProperty, value); }
}

<TreeView x:Name="trePages" Grid.Row="2" Grid.Column="0" ItemsSource="{Binding Project,ElementName=root,Converter={StaticResource dbg},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource ProjectTemplate}"/>
A: 

I suspect it's got something to do with the nested Hier-data-templ in ProjectTemplate.

Just copied your source into a blank project and toyed a bit.. Here's what worked for me.

<Window.Resources>
      <HierarchicalDataTemplate  x:Key="ProjectItemTemplate" ItemsSource="{Binding Pages}">
         <TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Foreground="Red"/>
      </HierarchicalDataTemplate>
      <HierarchicalDataTemplate x:Key="ProjectTemplate" ItemsSource="{Binding Pages}" ItemTemplate="{StaticResource ProjectItemTemplate}">
         <StackPanel>
            <TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
         </StackPanel>
      </HierarchicalDataTemplate>

   </Window.Resources>
    <Grid>
      <TreeView x:Name="trePages" ItemsSource="{Binding Projects}" ItemTemplate="{StaticResource ProjectTemplate}" />
   </Grid>
Gishu
Thanks.Binding to the 'Project.Pages' property works, but then I don't get the extra attributes/properties from the Project class. There is only one Project loaded at a time if that makes a difference - your example uses 'Projects'.
Echilon
The TreeView is an "ItemsContol" derivation. It can only display lists... which means for your case.. you'd have to stick your single project object into a list before pointing the TreeView at it. Give it a try.
Gishu
You're right really, it should be used to display Items. I've swapped the controls around a bit and am using a few TextBlocks instead.Thanks.
Echilon