开发者

WPF TreeView Never Populates Using HierarchicalDataTemplate

开发者 https://www.devze.com 2023-01-26 06:10 出处:网络
With the code below my treeview never populates. Can anyone see waht I\'m doing wrong? thanks public class FouList

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开发者_JS百科 and can see it populating correctly
treeView1.DataContext = FL;


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"/>


I would try either changing your

List<FouData> ListOfFou  { get; set; } 

to

ObservableCollection<FouData> ListOfFou { get; set; }

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

0

精彩评论

暂无评论...
验证码 换一张
取 消