2016-12-01 15 views
0

TreeViewの2つのレベルをWPFにしたいが、2番目のレベルは表示されません。WPF TreeViewバインディングで上位階層のみが表示される

マイclassイスト

public class MyClass : BaseViewModel 
{ 
    #region Fields 

    public ObservableCollection<MySubClass> SubClassCollection 
    { 
     get; 
     private set; 
    } 

    #endregion Fields 

    #region Propertys 

    public string Name 
    { 
     get 
     { 
      return Model.Name; 
     } 
    } 

    #endregion Propertys 
} 

最初にし、私は私のMySubClass

public class MySubClass : BaseViewModel 
{ 
    #region Propertys 


    public string Name 
    { 
     get 
     { 
      return Model.Name; 
     } 
    } 

    public int Number 
    { 
     get 
     { 
      return Model.Number; 
     } 
    } 
    #endregion Propertys 

} 

そして、私のXAMLは次のようになりました:

<TreeView ItemsSource="{Binding Path=MyClassCollection, Mode=OneWay}"> 
    <TreeView.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type vm:MyClass}"> 
      <Grid> 
       <TextBlock Text="{Binding Name}" Foreground="{DynamicResource AccentColorBrush}" /> 
      </Grid> 
     </HierarchicalDataTemplate> 
     <DataTemplate DataType="{x:Type vm:MySubClass}"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="19"></ColumnDefinition> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
       <TextBlock Text="{Binding Number}" 
          HorizontalAlignment="Left" 
          /> 
       <StackPanel Orientation="Horizontal" 
          Grid.Column="1" 
          HorizontalAlignment="Left" 
          > 
        <TextBlock Text=" (" 
           Foreground="{DynamicResource AccentColorBrush}" 
           /> 
        <TextBlock Text="{Binding Name}" 
           Foreground="{DynamicResource AccentColorBrush}" 
           /> 
        <TextBlock Text=")" 
           Foreground="{DynamicResource AccentColorBrush}" 
           /> 
       </StackPanel> 
      </Grid> 
     </DataTemplate> 
    </TreeView.Resources> 
</TreeView> 

しかし、それは私の唯一の一覧を示していMyClass相対なしMySubClass

私は間違っていますか? MyClassのため

答えて

1

HierarchicalDataTemplateも3つのテキストブロックが

<TextBlock Text="{Binding Name, StringFormat=' (\{0\})'}" 
      Foreground="{DynamicResource AccentColorBrush}" /> 
を行います StringFormatパラメータと

<TextBlock Text=" (" 
      Foreground="{DynamicResource AccentColorBrush}"/> 
<TextBlock Text="{Binding Name}" 
      Foreground="{DynamicResource AccentColorBrush}"/> 
<TextBlock Text=")" 
      Foreground="{DynamicResource AccentColorBrush}"/> 

1のTextBlock冗長で入れ子レベル

<HierarchicalDataTemplate DataType="{x:Type vm:MyClass}" 
          ItemsSource="{Binding Path=SubClassCollection}"> 


のためのデータを提供する結合のItemsSourceが欠落しています

おそらくStackPanelも冗長になります

+0

ありがとうございます! – Peter

関連する問題