[英]WPF TreeView binding shows only top hierachy
I want to have a two level TreeView
in WPF
, but it doesn't show the second level. 我想在WPF
有一个二级TreeView
,但是它不显示第二级。
My first class
ist 我的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
}
And then i got my MySubClass
然后我得到了MySubClass
public class MySubClass : BaseViewModel
{
#region Propertys
public string Name
{
get
{
return Model.Name;
}
}
public int Number
{
get
{
return Model.Number;
}
}
#endregion Propertys
}
And my XAML
looks like this: 我的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>
But it shows me only a List of MyClass
without the relative MySubClass
. 但是它只显示MyClass
的列表,而没有相对的MySubClass
。
What am I doing wrong? 我究竟做错了什么?
HierarchicalDataTemplate for MyClass is missing ItemsSource binding which provide data for nested levels MyClass的HierarchicalDataTemplate缺少为嵌套级别提供数据的ItemsSource绑定
<HierarchicalDataTemplate DataType="{x:Type vm:MyClass}"
ItemsSource="{Binding Path=SubClassCollection}">
<TextBlock Text="{Binding Name, StringFormat=' (\{0\})'}"
Foreground="{DynamicResource AccentColorBrush}" />
1 TextBlock with a StringFormat
parameter will do 1个具有StringFormat
参数的TextBlock将执行
<TextBlock Text="{Binding Name, StringFormat=' (\\{0\\})'}" Foreground="{DynamicResource AccentColorBrush}" />
that probably makes StackPanel
redundant as well 这也可能会使StackPanel
变得多余
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.