Reputation: 2388
I have a strange problem with a WPF treeview. In my viewmodel I am doing this:
private ObservableCollection<ITreeItem> _Tree = new ObservableCollection<ITreeItem>();
public ObservableCollection<ITreeItem> Tree
{
get { return this._Tree; }
}
Now when I add some dummy Parent / child data in the constructor of the viewmodel like this:
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;
the tree is displaying correctly.
However, as soon as I am adding some child items via a method (with dispatcher) they do not show at all. When i call this method, for example, only the root node is showing.
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;
})
);
}
Here is the XAML for the treeview
<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>
Upvotes: 1
Views: 1427
Reputation: 69959
It appears that you have got your naming mixed up. In your code, you show this line:
parent.Childs.Add(new Child { Name= "test", Description= "test2" });
This makes me think that your data type has a Childs
property. But then in your XAML, you have this:
<HierarchicalDataTemplate x:Key="hDataTemplate" ItemsSource="{Binding ChildSteps}">
...
</HierarchicalDataTemplate>
I'm guessing that one of these is wrong. So, try this XAML instead:
<HierarchicalDataTemplate x:Key="hDataTemplate" ItemsSource="{Binding Childs}">
...
</HierarchicalDataTemplate>
Upvotes: 1