WPF 树视图在未在 VM 的构造函数中添加子节点时不显示子节点



我对WPF树视图有一个奇怪的问题。在我的视图模型中,我这样做:

private ObservableCollection<ITreeItem> _Tree = new ObservableCollection<ITreeItem>();
public ObservableCollection<ITreeItem> Tree
{
    get { return this._Tree; }
}

现在,当我在视图模型的构造函数中添加一些伪父/子数据时,如下所示:

var parent = new ParentItem(1, "test", "test2");
this.Tree.Add(parent);
for (int i = 0; i < 10; i++)
{
    parent.Childs.Add(new Child { Name= "test", Description= "test2" });
}
parent.IsExpanded = true;

树显示正确。

然而,当我通过一个方法(使用dispatcher)添加一些子项时,它们根本不会显示。例如,当我调用这个方法时,只显示根节点。

public void Update()
{
    DispatcherSingleton.Instance.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, 
        new Action(delegate
        {
            var parent = new ParentItem(1, "test", "test2");
            this.Tree.Add(parent);
            for (int i = 0; i < 10; i++)
            {
                parent.Childs.Add(new Child { Name= "test", Description= "test2" });
            }
            parent.IsExpanded = true;
        })
    );
}

这是树视图的XAML

<ResourceDictionary>
    <Style TargetType="TreeViewItem" x:Key="itmContStyle">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
    </Style>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
    <HierarchicalDataTemplate x:Key="hDataTemplate"  ItemsSource="{Binding Childs}">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding Image, Mode=TwoWay}" Margin="1" />
            <Image x:Name="currentImage" Source="/WpfApp;component/Resources/Images/ac.png" Visibility="Collapsed"></Image>
            <TextBlock Text="{Binding SortOrder}" Margin="1" />
            <TextBlock Text="." Margin="0,1,1,0" />
            <TextBlock Text="{Binding Bezeichnung}" Margin="1" />
        </StackPanel>
        <HierarchicalDataTemplate.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsSelected}" Value="True">
                <Setter TargetName="currentImage" Property="Visibility" Value="Visible"/>
            </DataTrigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>
</ResourceDictionary>
...
<TreeView  HorizontalAlignment="Stretch"  VerticalAlignment="Top" 
           BorderThickness="0" 
           ItemsSource="{Binding Tree}"            
           ItemContainerStyle="{StaticResource itmContStyle}" ItemTemplate="{StaticResource hDataTemplate}">
</TreeView>

您的命名似乎混淆了。在代码中,您显示以下行:

parent.Childs.Add(new Child { Name= "test", Description= "test2" });

这使我认为您的数据类型具有Childs属性。但在XAML中,您有以下内容:

<HierarchicalDataTemplate x:Key="hDataTemplate"  ItemsSource="{Binding ChildSteps}">
    ...
</HierarchicalDataTemplate>

我猜其中一个是错的。所以,试试这个XAML:

<HierarchicalDataTemplate x:Key="hDataTemplate"  ItemsSource="{Binding Childs}">
    ...
</HierarchicalDataTemplate>