WPF TreeView永远不会使用HierarchicalDataTemplate填充

时间:2010-11-20 15:03:15

标签: wpf treeview hierarchicaldatatemplate

使用下面的代码,我的树视图永远不会填充。 谁能看到我做错了什么?

感谢

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;

2 个答案:

答案 0 :(得分:1)

ItemsSource

treeView1绑定不正确。我想你打算绑定到属性ListOfFou,而不是绑定到FouList

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

答案 1 :(得分:0)

我会尝试改变你的

List<FouData> ListOfFou  { get; set; } 

ObservableCollection<FouData> ListOfFou { get; set; }

或使用NotifyPropertyChanged("ListOfFou");

传播更改