Hi,@BabyHai. Welcome Microsoft Q&A. For the problem of bind the command to TreeViewItem, you could try to refer to the following code.
<TreeView Name="templateTreeview" Grid.Column="1"
ItemsSource="{Binding TreeViewSource}" Width="200" FontSize="14" AllowDrop="True" Margin="5,5,5,10">
<i:Interaction.Behaviors>
<local:TreeViewDragDropBehavior />
</i:Interaction.Behaviors>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:ViewItem}" ItemsSource="{Binding Path=ViewItems}">
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="MyControl" Text="{Binding Path=ItemName}" Margin="0,0,10,0" />
</StackPanel>
</HierarchicalDataTemplate>
<ContextMenu x:Key="FirstLevelContextMenu" Focusable="False">
<MenuItem Header="1.5A0" Focusable="False" Command="{Binding DataContext.A01Command, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" CommandParameter="{Binding }" />
<MenuItem Header="Ctx1" Focusable="False" Command="{Binding DataContext.Ctx1Command, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" CommandParameter="{Binding}" />
<MenuItem Header="Ctx2" Focusable="False" Command="{Binding DataContext.ContextMenuItemCommand, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" CommandParameter="{Binding}" />
<MenuItem Header="Ctx3" Focusable="False" Command="{Binding DataContext.ContextMenuItemCommand, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" CommandParameter="{Binding}" />
</ContextMenu>
</TreeView.Resources>
</TreeView>
Codebedhind:
public class MainViewModel
{
public ObservableCollection<ViewItem> ListBoxSource { get; set; }
public ObservableCollection<ViewItem> TreeViewSource { get; set; }
public RelayCommand A01Command { get; private set; }
public RelayCommand Ctx1Command { get; private set; }
public MainViewModel()
{
...
A01Command = new RelayCommand(ExecuteA01Command);
Ctx1Command = new RelayCommand(ExecuteCtx1Command);
}
private void ExecuteA01Command(object parameter)
{
if (parameter is ViewItem viewItem)
{
MessageBox.Show($"Clicked on '{viewItem.ItemName}'");
}
}
private void ExecuteCtx1Command(object parameter)
{
other action
}
}
RelayCommand:
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.