Hi Roger,
in PerformLogin(win As Object) you can check parameter "win" where you get CommandParameter. Try following demo:
<Window x:Class="WpfApp1.Window046"
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:WpfApp046"
mc:Ignorable="d"
Title="Roger Schlueter_230313" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<Label x:Name="Login" Content="First Element for Login" Margin="10"/>
<Label x:Name="Logout" Content="Second Element for Logout" Margin="10"/>
<Button
Command="{Binding Path=Login}"
CommandParameter="{Binding ElementName=Login}"
Content="Login" Margin="10"/>
<Button
Command="{Binding Path=Login}"
CommandParameter="{Binding ElementName=Logout}"
Content="Logout" Margin="10"/>
</StackPanel>
</Window>
ViewModel:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApp046
{
public class ViewModel
{
public ICommand Login { get => new RelayCommand(CmdExec, CanCmdExec); }
private void CmdExec(object obj)
{
var element = obj as Label;
switch (element?.Name)
{
case "Login":
MessageBox.Show("Login pressed");
break;
case "Logout":
MessageBox.Show("Logout pressed");
break;
default:
break;
}
}
private bool CanCmdExec(object obj) => true;
}
#pragma warning disable CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
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; }
}
}
}
Result: