WPF TreeView绑定仅显示顶级层次结构

时间:2016-12-01 09:43:53

标签: c# wpf xaml treeview

我希望在TreeView中有两个级别WPF,但它不显示第二级。

我的第一个class ist

 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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

MyClass的HierarchicalDataTemplate缺少ItemsSource绑定,它为嵌套级别提供数据

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

<小时/> 3个TextBlocks也是多余的

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

带有StringFormat参数的1个TextBlock将执行

<TextBlock Text="{Binding Name, StringFormat=' (\{0\})'}" 
           Foreground="{DynamicResource AccentColorBrush}"  />

可能会使StackPanel多余