Itemscontrol display only first element of the collection - c#

I have ItemControl
<ItemsControl ItemsSource="{Binding CanvasCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Width="{Binding CanvasSize}" Height="{Binding CanvasSize}" Background="RoyalBlue" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Top" Value="{Binding Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type viewModels:VertexViewModel}">
<Thumb Width="11" Height="11">
<Thumb.Template>
<ControlTemplate>
<Ellipse Width="11" Height="11" Fill="{Binding Fill}" />
</ControlTemplate>
</Thumb.Template>
</Thumb>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And composite collection with currently one ObservableCollection:
private ObservableCollection<VertexViewModel> Points { get; set; }
public CompositeCollection CanvasCollection { get; set; }
While debugging, I see that collection contains two elements, as I accepted, but only first is displaying on Canvas. When I call Refresh() method in model, I see that binding working, but only for first element.
Adding Point to CompositeCollection:
Points = new ObservableCollection<VertexViewModel>();
CanvasCollection = new CompositeCollection()
{
Points
};

I had to use container for collectoin:
CanvasCollection = new CompositeCollection
{
new CollectionContainer() {Collection = Points},
Polygon,
};

Related

ListBox HasItems DataTrigger not working when Items are cleared from ItemsSource

I have a Listbox and I have a DataTrigger in xaml to check if there are no items in the ListBox then Hide it (Please see the HasItems DataTrigger). At some point in my application I am Clearing the items from the ItemsSource i.e FilteredVolumesList (Please see the DeSelectAssays method) assuming the ListBox would be hidden because there are no Items In the ItemsSource.
But the problem is the DataTrigger dosen't seem to work and the ListBox is not being collapsed. Please help. I dont want to create a bool property for visibility and keep setting to false. Please note I am doing something like this in xaml ListBox CLear Items Xamly
Here is my xaml
<ListBox
Grid.Row="1"
x:Name="LstVolumes"
HorizontalAlignment="Center"
Margin="0,90,0,0"
VerticalAlignment="Top"
ItemsSource="{Binding FilteredVolumesList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
SelectionChanged="LstVolumes_SelectionChanged"
BorderThickness="0">
<ListBox.Style>
<Style
TargetType="{x:Type ListBox}"
BasedOn="{StaticResource ListBoxDarkBackgroundStyle}">
<Setter
Property="Template"
Value="{StaticResource ListBoxVolumesTemplate}"></Setter>
<Style.Triggers>
<DataTrigger
Binding="{Binding HasItems,RelativeSource={RelativeSource Self},UpdateSourceTrigger=PropertyChanged}"
Value="False">
<Setter
Property="Visibility"
Value="Collapsed"></Setter>
</DataTrigger>
<DataTrigger
Binding="{Binding DataContext.IsVolumeRepeaterVisible,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}"
Value="False">
<Setter
Property="Template"
Value="{StaticResource ListBoxNoRepeaterTemplate}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel
Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Button
x:Name="VolumesButton"
Style="{StaticResource ButtonLightTextGradientDarkBackgroundStyle}"
Margin="{StaticResource AllControlsMargin}"
Click="VolumesButton_Click">
<Button.Content>
<StackPanel
x:Name="stkInnerChildPanel"
Orientation="Vertical"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock
Style="{StaticResource TextBlockLightTextStyle}"
x:Name="TblVolume"
Text="{Binding volume_display_value,StringFormat={}{0} µg}"></TextBlock>
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And I am clearing the items like this from the code behind
private ReadJsonAssayViewModel AssayViewModel = null;
public RunSetUpMainContentUserControl()
{
InitializeComponent();
AssayViewModel = ReadJsonAssayViewModel.ReadJsonAssayViewModelInstance;
this.DataContext = AssayViewModel;
FindAssociatedChildrenInstance = FindChildrenAndAssociatedControlsHelper.FindAssociateChildrenInstance;
Messenger.Default.Register<NotificationMessage>(this, DeSelectAssays);
}
private void DeSelectAssays(NotificationMessage obj)
{
for (int i = 0; i < LstAssays.Items.Count; i++)
{
ToggleButton btn = FindAssociatedChildrenInstance.AssociatedPanel(LstAssays, "AssaysButton", i, TargetType.ListAssays, typeof(ToggleButton)) as ToggleButton;
btn.IsChecked = false;
}
LstAssays.SelectedIndex = -1;
AssayViewModel.FilteredVolumesList.Clear();
AssayViewModel.IsStartRunEnabled = false;
this.Focus();
}
Here is the FilteredVolumesList in the viewmodel
private List<Volume> _filteredVolumesList;
public List<Volume> FilteredVolumesList
{
get
{
return _filteredVolumesList;
}
set
{
_filteredVolumesList = value;
OnPropertyChanged("FilteredVolumesList");
}
}

Creating an Expand All and Collapse All Buttons with Expander in WPF

I am working in Visual Studio 2013 in WPF (C#) and I have the following code to create an expander from my xml. It is currently working perfectly right now but I want to include an expand all and collapse all buttons. I have looked all over and can't seem to find a solution.
Here is where the expander is created. I know I just have to iterate through a list of items and change the Property="IsExpanded" to Value="True" to create an expand all.
<DataTemplate x:Key="dtListTemplate" >
<StackPanel>
<Expander LostFocus="CollapseExpander" ExpandDirection="Down" Width="Auto">
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="IsExpanded" Value="False" />
<Setter Property="Header" Value="{Binding XPath=#Name}" />
<Setter Property="FontWeight" Value="Bold"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsExpanded,RelativeSource={RelativeSource Self}}" Value="True">
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<ListBox Name="itemsList"
ItemsSource="{Binding XPath=UpgradeAction}"
ItemTemplate="{StaticResource dtListItemTemplate}"
SelectionChanged="listItems_SelectionChanged"
Style="{StaticResource styleListBoxUpgradeAction}"
ItemContainerStyle="{StaticResource styleListBoxItemUpgradeAction}">
</ListBox>
</Expander>
</StackPanel>
</DataTemplate>
Here's the code that calls the DataTemplate which has information from an Xml.
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Border Grid.Column="0" Grid.Row="0" Width="790" Height="40" Padding="5" Background="#4E87D4">
<Label VerticalAlignment="Center" FontSize="16" FontWeight="Bold" Foreground="White">Test</Label>
</Border>
<Button Name="ExpandBttn" Width="100" Height="40" FontSize="16" FontWeight="Bold" Content="Expand All" DataContext="{Binding}" Click="Expand_Button_Click"/>
<Button Name="ColapseBttn" Width="100" Height="40" FontSize="16" FontWeight="Bold" Content="Colapse All" DataContext="{Binding}" Click="Collapse_Button_Click"/>
</StackPanel>
<ListView Name="listItems" Grid.Column="0" Grid.Row="1" Background="Wheat"
ItemsSource="{Binding Source={StaticResource xmldpUpgradeActions}, XPath=ActionGroup}"
ItemTemplate="{StaticResource dtListTemplateRichards}"
SelectionChanged="listItems_SelectionChanged">
</ListView>
</StackPanel>
Here's what I tried in the .cs file for the expand all portion.
private void Expand_Button_Click(object sender, RoutedEventArgs e)
{
foreach(var item in listItems.Items)
{
var listBoxItem = listItems.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
var itemExpander = (Expander)GetExpander(listBoxItem);
if (itemExpander != null)
itemExpander.IsExpanded = true;
}
}
private static DependencyObject GetExpander(DependencyObject container)
{
if (container is Expander) return container;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(container); i++)
{
var child = VisualTreeHelper.GetChild(container, i);
var result = GetExpander(child);
if (result != null)
{
return result;
}
}
return null;
}
Any help is greatly appreciated!
Is xmldpUpgradeActions a CollectionViewSource?
Whatever class is in the collection, it should implement INotifyPropertyChanged.
Give it an IsExpanded property that raises PropertyChanged in its setter when its value changes, and bind that to Expander.IsExpanded in the template:
<Expander
IsExpanded="{Binding IsExpanded}"
LostFocus="CollapseExpander"
ExpandDirection="Down"
Width="Auto">
Write a command that loops through all the items in the collection and sets item.IsExpanded = false; on each one.

WPF ListView inside Grouped TreeView not working

In a WPF Project I have a set of record objects with the properties School, Subject, FirstName and LastName. The records are grouped by School and Subject using CollectionViewSource in XAML and this is used by a TreeView to show the grouped items. The groupings work fine.
The problem is I would like to show the FirstName and LastName of the records under the Subject in a ListView using a GridView as its View with FirstName and LastName as columns, but I can't figure out how to do this.
Here is an image of the current display:
Here's some sample code to show what I mean.
public class Record
{
public string School { get; set; }
public string Subject { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var records = new Record[]
{
new Record() { School = "School A", Subject = "Maths" , FirstName = "Fred" , LastName = "Blogs"},
new Record() { School = "School A", Subject = "English" , FirstName = "Alice" , LastName = "Lane"},
new Record() { School = "School B", Subject = "Geography" , FirstName = "John" , LastName = "Smith"},
new Record() { School = "School B", Subject = "Geography" , FirstName = "Burt" , LastName = "Lancaster"},
new Record() { School = "School C", Subject = "Chemistry" , FirstName = "Dee" , LastName = "Kaye"}
};
this.DataContext = records;
}
}
XAML :
<Window x:Class="ListViewInTreeView.MainWindow"
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:local="clr-namespace:ListViewInTreeView"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource x:Key="listings"
Source="{Binding .}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="School" />
<PropertyGroupDescription PropertyName="Subject" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<TreeView Grid.Row="4" DataContext="{StaticResource listings}" ItemsSource="{Binding}" >
<TreeView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Margin="0,0,0,0" IsExpanded="True" BorderBrush="#FFA4B97F"
BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
<TextBlock Text=" : "/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
<TextBlock Text=" items(s)"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter Margin="20,0,0,0" />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</TreeView.GroupStyle>
<TreeView.Resources>
<!-- NEED HELP HERE I THINK ?-->
<HierarchicalDataTemplate DataType="{x:Type GroupItem}">
<ListView ItemsSource="{Binding .}">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
</GridView>
</ListView.View>
</ListView>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
Use 2 GroupStyles, one for the School group and another one for the Subject group:
<TreeView Grid.Row="4" DataContext="{StaticResource listings}" ItemsSource="{Binding}" >
<TreeView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Margin="0,0,0,0" IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
<TextBlock Text=" : "/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
<TextBlock Text=" items(s)"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter Margin="20,0,0,0" />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Margin="0,0,0,0" IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
<TextBlock Text=" : "/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
<TextBlock Text=" items(s)"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
</GridView>
</ListView.View>
</ListView>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</TreeView.GroupStyle>
</TreeView>
You probably also want to move the common property settings for the Expander and GroupItem to two styles that are used by both GroupStyles:
<TreeView Grid.Row="4" DataContext="{StaticResource listings}" ItemsSource="{Binding}" >
<TreeView.Resources>
<Style TargetType="GroupItem">
<Setter Property="Margin" Value="0,0,0,5"/>
</Style>
<Style TargetType="Expander">
<Setter Property="Margin" Value="0,0,0,0" />
<Setter Property="IsExpanded" Value="True" />
<Setter Property="BorderBrush" Value="#FFA4B97F" />
<Setter Property="BorderThickness" Value="0,0,0,1" />
<Setter Property="Header" Value="{Binding}" />
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
<TextBlock Text=" : "/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
<TextBlock Text=" items(s)"/>
</DockPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
<TreeView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Content>
<ItemsPresenter Margin="20,0,0,0" />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Content>
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
</GridView>
</ListView.View>
</ListView>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</TreeView.GroupStyle>
</TreeView>

Grouping data into itemscontrol(s) with template

following some examples and blogs, I've done a little project to test the grouping of some elements in my xaml. The code is:
<Window.Resources>
<XmlDataProvider x:Key="data">
<x:XData>
<Devices xmlns="">
<Terminal name="Gasoline" Code="00001001" />
<Terminal name="cherosene" Code="00001002" />
<Terminal name="Oil" Code="00001002" />
<Terminal name="Wather" Code="00001003" />
<Terminal name="cherosene" Code="00001003" />
<Terminal name="Wather" Code="00001004" />
<Terminal name="cherosene" Code="00001004" />
<Terminal name="Oil" Code="00001004" />
<Terminal name="cherosene" Code="00001004" />
<Terminal name="alcohol" Code="00001005" />
</Devices>
</x:XData>
</XmlDataProvider>
<CollectionViewSource x:Key="TerminalByCodes" Source="{Binding Source={StaticResource data}, XPath=Devices/Terminal}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="#Code" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<DockPanel>
<ScrollViewer DockPanel.Dock="Bottom" VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Source={StaticResource TerminalByCodes}}" >
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<GroupBox Header="{Binding Name}">
<ItemsPresenter/>
</GroupBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ItemsControl.GroupStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=#name}" Background="#FFDBA8A8" Margin="0,0,10,0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</DockPanel>
</Grid>
This code has this output:
As you can see, the data are written in the xaml. But, as you imagine, this isn't how I have to work. How can I update my code to make it work with "code-generated-data"? And how if is it made with a mvvm binding?
Salve Piero !
You can instanciate CollectionViewsSource in a view model class :
CollectionViewSource viewSource = new CollectionViewSource();
and provide some data to it :
private List<Terminal> terminals = new List<Terminal>
{
new Terminal{ Code="00001001", Name= "Gasoline" },
new Terminal{ Code="00001001", Name= "cherosene"},
new Terminal{ Code="00001001", Name= "Oil"},
new Terminal{ Code="00001003", Name= "Gasoline" },
new Terminal{ Code="00001003", Name= "cherosene"},
new Terminal{ Code="00001003", Name= "Oil"},
};
terminalsViewSource.Source = terminals;
Create a property in a view model class so that Databinding can work :
public Object TerminalsView
{
get { return terminalsViewSource.View; }
}
In code behind you can create the ViewModel :
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
Bind to it in .xaml :
<DataGrid x:Name="datagridTerminals" ItemsSource="{Binding TerminalsView}" AutoGenerateColumns="True" >
Additionaly in Viewmodel you can add, Filter, IsSorted, IsGrouped properties so that your data can be filtered (by name for instance), grouped (or not), and sorted.
For instance :
<TextBox x:Name="textboxFilter" Text="{Binding Filter}" />
Property in ViewModel :
public String Filter
{
get { return filter; }
set
{
filter = value;
terminalsViewSource.View.Refresh();
}
}
All that works in the code sample here :
http://1drv.ms/1P8cMVc
Forza !

WPF treeview does not show child nodes when not adding them in vm's constructor

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>
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>

Categories