0

我试图在 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>

这是输出的样子。不应显示任何重复项: 输出图片

4

1 回答 1

0

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

productGroupViewSource.Filter += productGroupViewSource_Filter;

void productGroupViewSource_Filter(object sender, FilterEventArgs e)
{
    var node = (ProductGroup) e.Item;
    e.Accepted = node.Parent == null;
}
于 2013-03-04T19:53:50.273 回答