2010-11-20 5 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

0

私はあなたの

List<FouData> ListOfFou { get; set; } 

ObservableCollection<FouData> ListOfFou { get; set; } 

に変更するか、NotifyPropertyChanged("ListOfFou");

+0

一般的には良いアドバイスですが、コレクションが変更されたときにツリーを更新しない限りは不要です。 'DataContext'が変更されたときにツリーが更新されるため、サンプルコードが有効です。 – Athari

関連する問題