Wpf TreeView多级层次结构

时间:2017-02-09 13:33:54

标签: c# wpf xaml

我有这个课程

 public class Country
{
    [Key]
    public int CountryId { get; set; }

    public string Name { get; set; }

    public int? ParentId { get; set; }

    [ForeignKey("ParentId")]
    public Country Parent { get; set; }

    public List<Supplier> Suppliers { get; set; }

    public ObservableCollection<Country> SubCountries { get; set; }
}

想要在多层次结构中的TreeView控件中查看集合

这是Xaml代码

    <Window.Resources>
        <HierarchicalDataTemplate x:Key="TreeViewTemp"  DataType="TreeViewItem"  ItemsSource="{Binding SubCountries}">
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>
    </Window.Resources>


     <TreeView Name="TreeView_Countries" ItemTemplate="{StaticResource TreeViewTemp}" Grid.RowSpan="2" Margin="510,14,-510,-14">


 </TreeView>

主要国家完美地装载了完美的层次结构但问题是SubCountreis再次加载其SubCountries下面.. 任何帮助!!

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案..实际上非常简单......

我所要做的就是过滤这些物品 源集合仅限于父国

TreeView_Countries.ItemsSource = collection.Where(x => x.ParentId == null);