我希望在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
。
我做错了什么?
答案 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
多余