Question

With the code below my treeview never populates. Can anyone see waht I'm doing wrong?

thanks

public class FouList
{
  public string Source { get; set; }
  public List<FouData> ListOfFou { get; set; } 
}

 public struct FouData
{
   public string Name { get; set; }
}

<Window.Resources>
 <HierarchicalDataTemplate DataType="{x:Type local:FouList}"
                              ItemsSource="{Binding Path=ListOfFou}">
    <Border BorderBrush="Black"
                BorderThickness="2"
                CornerRadius="10">            
            <TextBlock Margin="10,0,0,0"
                           Text="{Binding Source}"></TextBlock>

    </Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:FouData}">
    <Border BorderBrush="Black"
                BorderThickness="2"
                CornerRadius="10">           
            <TextBlock Margin="10,0,0,0"
                           Text="{Binding Name}"></TextBlock>               

    </Border>
</HierarchicalDataTemplate>
</Window.Resources>


<TreeView Margin="26,0,35,12" Name="treeView1" Height="298"
   ItemsSource="{Binding Path=FouList}" VerticalAlignment="Top"/>

FouList FL = new FouList();
//code to populate FL
//I've debugged and can see it populating correctly
treeView1.DataContext = FL;
Was it helpful?

Solution

ItemsSource binding of treeView1 is incorrect. I suppose you intend to bind to the property ListOfFou, not to FouList.

<TreeView Margin="26,0,35,12" Name="treeView1" Height="298"
          ItemsSource="{Binding Path=ListOfFou}" VerticalAlignment="Top"/>

OTHER TIPS

I would try either changing your

List<FouData> ListOfFou  { get; set; } 

to

ObservableCollection<FouData> ListOfFou { get; set; }

or propagating the change with NotifyPropertyChanged("ListOfFou");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top