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

时间:2013-12-13 10:32:28

标签: c# wpf xaml mvvm treeview

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

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

现在,我在viewmodel的构造函数中添加一些虚拟的父/子数据,如下所示:

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;

树正确显示。

但是,只要我通过方法(使用调度程序)添加一些子项目,它们就不会显示。例如,当我调用此方法时,只显示根节点。

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>

1 个答案:

答案 0 :(得分:1)

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

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>