Hi,
try following demo with your code sequences:
XAML:
<Window x:Class="WpfApp1.Window106"
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:WpfApp106"
mc:Ignorable="d"
Title="Window106" Height="450" Width="800">
<Window.Resources>
<local:ViewModel x:Key="vm"/>
<ContextMenu x:Key="contextMenu">
<MenuItem Header="View"
Command="{Binding View_OnClick, Source={StaticResource vm}}"
CommandParameter="{Binding}"/>
<MenuItem Header="Edit"
Command="{Binding Edit_OnClick, Source={StaticResource vm}}"
CommandParameter="{Binding}" />
<MenuItem Header="Delete"
Command="{Binding Delete_OnClick, Source={StaticResource vm}}"
CommandParameter="{Binding}"/>
</ContextMenu>
</Window.Resources>
<Grid DataContext="{StaticResource vm}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="318*" />
</Grid.ColumnDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Disabled">
<StackPanel Grid.Column="0"
Background="#4682b4"
Margin="0,0,0,-0.2" >
<Button Content="New Connection"
Command="{Binding BtnAdd_OnClick}"
CommandParameter="{StaticResource contextMenu}"/>
<ListBox ItemsSource="{Binding View}"/>
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
and code:
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApp106
{
public class ViewModel
{
public ObservableCollection<Label> View { get; } = new ObservableCollection<Label>();
public ICommand BtnAdd_OnClick
{
get => new RelayCommand((state) =>
{
View.Add(new Label
{
Content = "New Connection",
ContextMenu = (ContextMenu)state
});
});
}
public ICommand View_OnClick
{
get => new RelayCommand((state) =>
{
var clickedItem = state as Label;
if (clickedItem != null)
{
MessageBox.Show(" Viewing: " + clickedItem.Content);
}
});
}
public ICommand Edit_OnClick
{
get => new RelayCommand((state) =>
{
var clickedItem = state as Label;
if (clickedItem != null)
{
string newName = "Connection edited on " + DateTime.Now.ToLongTimeString();
string oldName = Convert.ToString(clickedItem.Content);
clickedItem.Content = newName;
MessageBox.Show(string.Format("Changed name from '{0}' to '{1}'", oldName, newName));
}
});
}
public ICommand Delete_OnClick
{
get => new RelayCommand((state) =>
{
var clickedItem = state as Label;
if (clickedItem != null)
{
string oldName = Convert.ToString(clickedItem.Content);
View.Remove(clickedItem);
MessageBox.Show(string.Format("Removed '{0}'", oldName));
}
});
}
}
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _action;
public RelayCommand(Action<object> action) { _action = action; _canExecute = null; }
public RelayCommand(Action<object> action, Predicate<object> canExecute) { _action = action; _canExecute = canExecute; }
public void Execute(object o) => _action(o);
public bool CanExecute(object o) => _canExecute == null ? true : _canExecute(o);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}