2010-11-20 37 views
0

隨着下面的代碼,我的樹視圖永遠不會填充。 任何人都可以看到我做錯了嗎?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; 

回答

1

變化treeView1的綁定不正確。我想你打算綁定到屬性ListOfFou,而不是FouList

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

您的更改有效 – mike 2010-11-20 16:55:29

0

我會嘗試要麼改變你的

List<FouData> ListOfFou { get; set; } 

ObservableCollection<FouData> ListOfFou { get; set; } 

或傳播與NotifyPropertyChanged("ListOfFou");

+0

這是一個很好的建議,但它是沒有必要的,除非你想在更改集合時更新樹。示例代碼應該可以工作,因爲當DataContext被更改時,樹會被更新。 – Athari 2010-11-20 15:42:09

相關問題