在WPF中使用TreeView

时间:2013-03-01 22:57:02

标签: wpf treeview

我正在尝试在WPF中显示一个简单的树结构,它正在工作,但它显示了树的顶层的所有级别的节点。我在结构中总共有9个节点,但只有一个应该在根级别显示。在那个应该是两个节点,在那些一对,等等。在每个节点下,它确实显示正确的子节点,但另外它显示在顶部的所有节点。这是我的对象(这些是EF db对象,如果重要的话):

    public class ProductGroup
{
    public int ProductGroupId { get; set; }
    public ProductGroup Parent { get; set; }
    public string Description { get; set; }

    private ProductGroup() //not used, but needed to prevent EF error
    {
        Description = string.Empty;
    }
    public ProductGroup(string description)
    {
        Description = description;
    }
    public virtual ICollection<ProductGroup> Children { get; set; }

    public void Add(ProductGroup newItem)
    {
        newItem.Parent = this;
        if (Children == null)
            Children = new Collection<ProductGroup>();
        Children.Add(newItem);
    }
    public int Count
    {
        get { return Children.Count; }
    }
}

这是xaml:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:ConfiguratorDB="clr-namespace:ConfiguratorDB;assembly=ConfiguratorDB" mc:Ignorable="d" x:Class="ConfiguratorDBTest.Window2"
    xmlns:local="clr-namespace:ConfiguratorDB;assembly=ConfiguratorDB"
Title="Window2" Height="417" Width="712" Loaded="Window_Loaded_1">
<Window.Resources>
    <CollectionViewSource x:Key="productGroupViewSource" d:DesignSource="{d:DesignInstance {x:Type ConfiguratorDB:ProductGroup}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource productGroupViewSource}">
    <DataGrid x:Name="productGroupDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="23,36,346,151" RowDetailsVisibilityMode="VisibleWhenSelected">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="descriptionColumn" Binding="{Binding Description}" Header="Description" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="productGroupIdColumn" Binding="{Binding ProductGroupId}" Header="Product Group Id" Width="SizeToHeader"/>
        </DataGrid.Columns>
    </DataGrid>
    <TreeView x:Name="productGroupTreeView" ItemsSource="{Binding}" Margin="396,38,73,111">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:ProductGroup}" ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Path=Description}" />
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</Grid>

这是输出的样子。不应该显示任何重复: Picture of Output

1 个答案:

答案 0 :(得分:0)

我能够通过根据节点是否是根节点过滤CollectionViewSource来正确显示树视图。基本上你只想要返回root。子级将由分层数据模板处理。以下是我添加到窗口代码隐藏的代码行:

productGroupViewSource.Filter += productGroupViewSource_Filter;

void productGroupViewSource_Filter(object sender, FilterEventArgs e)
{
    var node = (ProductGroup) e.Item;
    e.Accepted = node.Parent == null;
}