C# Treeview没有给我的孩子们看
我以前没有使用过TreeView,只是在一些教程中使用过。我以为我有,结果我没有 我正在尝试将我的TreeView绑定到一个对象 对象是C# Treeview没有给我的孩子们看,c#,wpf,xaml,treeview,C#,Wpf,Xaml,Treeview,我以前没有使用过TreeView,只是在一些教程中使用过。我以为我有,结果我没有 我正在尝试将我的TreeView绑定到一个对象 对象是 public List<MyGrandparents> MyGrandParents {get;set;} 公共列表我的祖父母{get;set;} 在MyGrandParent类中,有一个属性 public List<MyParents> MyParents{get;set;} public List<MyChildren&
public List<MyGrandparents> MyGrandParents {get;set;}
公共列表我的祖父母{get;set;}
在MyGrandParent类中,有一个属性
public List<MyParents> MyParents{get;set;}
public List<MyChildren> MyChildren {get;set;}
公共列表MyParents{get;set;}
最后,在MyParent类中有一个属性
public List<MyParents> MyParents{get;set;}
public List<MyChildren> MyChildren {get;set;}
公共列表MyChildren{get;set;}
在每个类中,还有其他属性,例如Name(虽然没有基类,但在这个阶段没有共享代码)
我想将地块绑定到树状视图,因此在“根”级别,我只看到祖父母。我可以扩大祖父母的范围,只看父母,我也可以扩大他们的范围,看孩子
我的问题是,只有最高级别的约束(祖父母级别)。我的XAML是
<TreeView ItemsSource="{Binding MyGrandParents}">
<TreeView.Resources>
<DataTemplate DataType="{x:Type local:MyGrandParent}">
<TextBlock Text="{Binding Name}" Margin="0,0,10,0" />
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:MyParent}" ItemsSource="{Binding MyGrandParents}">
<TextBlock Text="Don't bind to test"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
我不明白为什么这不能给我一棵漂亮的树。您没有按照正确的模式使用
HierarchycalDataTemplate
可能包含子项的项
应声明为HierarchycalDataTemplate
,并将ItemsSource
设置为子集合
,以便在其下方显示
不包含任何子项的项应声明为DataTemplate
应该是这样的-
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:MyGrandParent}"
ItemsSource="{Binding MyParents}">
<TextBlock Text="{Binding Name}" Margin="0,0,10,0" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:MyParent}"
ItemsSource="{Binding MyChildren}">
<TextBlock Text="Don't bind to test"></TextBlock>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:MyChildren}">
<TextBlock Text="Don't bind to test"></TextBlock>
</DataTemplate>
</TreeView.Resources>
您没有按照正确的模式使用HierarchycalDataTemplate
可能包含子项的项
应声明为HierarchycalDataTemplate
,并将ItemsSource
设置为子集合
,以便在其下方显示
不包含任何子项的项应声明为DataTemplate
应该是这样的-
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:MyGrandParent}"
ItemsSource="{Binding MyParents}">
<TextBlock Text="{Binding Name}" Margin="0,0,10,0" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:MyParent}"
ItemsSource="{Binding MyChildren}">
<TextBlock Text="Don't bind to test"></TextBlock>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:MyChildren}">
<TextBlock Text="Don't bind to test"></TextBlock>
</DataTemplate>
</TreeView.Resources>
您没有按照正确的模式使用HierarchycalDataTemplate
可能包含子项的项
应声明为HierarchycalDataTemplate
,并将ItemsSource
设置为子集合
,以便在其下方显示
不包含任何子项的项应声明为DataTemplate
应该是这样的-
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:MyGrandParent}"
ItemsSource="{Binding MyParents}">
<TextBlock Text="{Binding Name}" Margin="0,0,10,0" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:MyParent}"
ItemsSource="{Binding MyChildren}">
<TextBlock Text="Don't bind to test"></TextBlock>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:MyChildren}">
<TextBlock Text="Don't bind to test"></TextBlock>
</DataTemplate>
</TreeView.Resources>
您没有按照正确的模式使用HierarchycalDataTemplate
可能包含子项的项
应声明为HierarchycalDataTemplate
,并将ItemsSource
设置为子集合
,以便在其下方显示
不包含任何子项的项应声明为DataTemplate
应该是这样的-
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:MyGrandParent}"
ItemsSource="{Binding MyParents}">
<TextBlock Text="{Binding Name}" Margin="0,0,10,0" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:MyParent}"
ItemsSource="{Binding MyChildren}">
<TextBlock Text="Don't bind to test"></TextBlock>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:MyChildren}">
<TextBlock Text="Don't bind to test"></TextBlock>
</DataTemplate>
</TreeView.Resources>
我是否正在尝试反向操作?儿童是最低级别,但在您的示例中,它似乎是最高级别?DataTemplate是否为“根”,所有HierarchycalDataTemplates是否为子级?更新了答案并进行了解释。看一看,谢谢!在过去的一个小时里,我一直在阅读教程,但没有一个教程清楚地解释了TreeView
的用法。我是不是在尝试反向操作?儿童是最低级别,但在您的示例中,它似乎是最高级别?DataTemplate是否为“根”,所有HierarchycalDataTemplates是否为子级?更新了答案并进行了解释。看一看,谢谢!在过去的一个小时里,我一直在阅读教程,但没有一个教程清楚地解释了TreeView
的用法。我是不是在尝试反向操作?儿童是最低级别,但在您的示例中,它似乎是最高级别?DataTemplate是否为“根”,所有HierarchycalDataTemplates是否为子级?更新了答案并进行了解释。看一看,谢谢!在过去的一个小时里,我一直在阅读教程,但没有一个教程清楚地解释了TreeView
的用法。我是不是在尝试反向操作?儿童是最低级别,但在您的示例中,它似乎是最高级别?DataTemplate是否为“根”,所有HierarchycalDataTemplates是否为子级?更新了答案并进行了解释。看一看,谢谢!在过去的一个小时里,我一直在阅读教程,但是没有一个教程清楚地解释了TreeView
的用法。