C# WPF treeview在未将子节点添加到vm';s构造函数
我对WPF树视图有一个奇怪的问题。在我的viewmodel中,我正在执行以下操作:C# WPF treeview在未将子节点添加到vm';s构造函数,c#,wpf,xaml,mvvm,treeview,C#,Wpf,Xaml,Mvvm,Treeview,我对WPF树视图有一个奇怪的问题。在我的viewmodel中,我正在执行以下操作: private ObservableCollection<ITreeItem> _Tree = new ObservableCollection<ITreeItem>(); public ObservableCollection<ITreeItem> Tree { get { return this._Tree; } } private observetecollec
private ObservableCollection<ITreeItem> _Tree = new ObservableCollection<ITreeItem>();
public ObservableCollection<ITreeItem> Tree
{
get { return this._Tree; }
}
private observetecollection _Tree=new observetecollection();
公共可观测收集树
{
获取{返回此。\u 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;
var parent=新的ParentItem(1,“测试”、“测试2”);
this.Tree.Add(父级);
对于(int i=0;i<10;i++)
{
parent.Childs.Add(新的子项{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;
})
);
}
public void Update()
{
DispatcherSingleton.Instance.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal,
新操作(委派)
{
var parent=新的ParentItem(1,“测试”、“测试2”);
this.Tree.Add(父级);
对于(int i=0;i<10;i++)
{
parent.Childs.Add(新的子项{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>
...
看来您的命名有误。在代码中,显示以下行:
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>
...