Hi, try following demo and accept the helpful as answer:
XAML:
<Window x:Class="Window012"
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:WpfApp1.WpfApp012"
mc:Ignorable="d"
Title="Window012" Height="450" Width="800">
<Window.Resources>
<local:ViewModel x:Key="vm"/>
</Window.Resources>
<Grid DataContext="{StaticResource vm}">
<TreeView ItemsSource="{Binding TreeViewDataCollection}">
<TreeView.Resources>
<ContextMenu x:Key="ChildMenu" >
<MenuItem Header="Get Configuration"
Command="{Binding GetConfigurationEvent, Source={StaticResource vm}}"
CommandParameter="{Binding}"/>
<MenuItem Header="Put Configuration"
Command="{Binding PutConfigurationEvent, Source={StaticResource vm}}"
CommandParameter="{Binding}"/>
</ContextMenu>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource ChildMenu}"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding HostGroupName}" DataType="{x:Type local:RootNameList}">
<Label Content="{Binding RootSubnet}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
ViewModel:
Imports System.ComponentModel
Namespace WpfApp012
Public Class ViewModel
Public Sub New()
For i = 1 To 10
Dim rnl As New RootNameList With {.RootSubnet = $"RootSubnet{i}"}
For k = 1 To 10
rnl.HostGroupName.Add(New RootNameList With {.RootSubnet = $"HostGroupName {k + i * 10}"})
Next
col.Add(rnl)
Next
cvs.Source = col
End Sub
Private cvs As New CollectionViewSource
Private col As New AsyncObservableCollection(Of RootNameList)
Public ReadOnly Property TreeViewDataCollection As ICollectionView
Get
Return cvs.View
End Get
End Property
Public ReadOnly Property GetConfigurationEvent As ICommand
Get
Return New RelayCommand(Sub(state)
Debug.WriteLine($"Get: {CType(state, RootNameList).RootSubnet}")
End Sub)
End Get
End Property
Public ReadOnly Property PutConfigurationEvent As ICommand
Get
Return New RelayCommand(Sub(state)
Debug.WriteLine($"Put: {CType(state, RootNameList).RootSubnet}")
End Sub)
End Get
End Property
End Class
Public Class RootNameList
Public Property RootSubnet As String
Public Property HostGroupName As New List(Of RootNameList)
End Class
End Namespace