According to the attached source you have all 3 commands as part of MainViewModel
public class MainViewModel : ViewModelBase
{
public RelayCommand RemoveTest { get; set; }
public RelayCommand RunTest { get; set; }
public RelayCommand AddTest { get; set; }
...
}
but you need to to remember that each child item will have different DataContext
then the whole TreeView
. Because you bind ItemsSource
to List<Test>
this means that DataContext
for each child TreeViewItem
will be set to different instance of Test
object and this class does not contain RemoveTest
nor RunTest
commands. Now you have 2 choices:
- move
RemoveTest
andRunTest
commands toTest
class - change binding source for these 2 commands so it points to
MainViewModel
<ContextMenu x:Key ="SolutionContext" StaysOpen="true">
<MenuItem Header="Remove" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Path=DataContext.RemoveTest}"/>
<MenuItem Header="Run" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Path=DataContext.RunTest}"/>
</ContextMenu>