WPF .Net Core Relay Command with Parameters
WPF
1
Posts
1
Posters
2
Views
1
Watching
-
Following [this SO article](https://stackoverflow.com/questions/34996198/the-name-commandmanager-does-not-exist-in-the-current-context-visual-studio-2), I'm trying to create a relay command that takes parameters:
<ctrls:MaroisHyperlink LinkText="Forgot Password?"
Foreground="SteelBlue"
Margin="30,0,0,0"
Height="20"><i:Interaction.Triggers> <i:EventTrigger EventName="LinkClicked"> <i:InvokeCommandAction Command="{Binding ForgotPasswordClickedCommand}" CommandParameter="True"/> </i:EventTrigger> </i:Interaction.Triggers>
</ctrls:MaroisHyperlink>
Here's my RelayCommand class:
namespace Marois.Framework.Core.Utilities
{
public class RelayCommand<T> : ICommand
{
private readonly Action<T> execute;private readonly Func<T, bool> canExecute; public RelayCommand(Action<T> execute) : this(execute, null) { } public RelayCommand(Action<T> execute, Func<T, bool> canExecute) { ArgumentNullException.ThrowIfNull(execute); this.execute = execute; this.canExecute = canExecute; this.RaiseCanExecuteChangedAction = RaiseCanExecuteChanged; SimpleCommandManager.AddRaiseCanExecuteChangedAction(ref RaiseCanExecuteChangedAction); } ~RelayCommand() { RemoveCommand(); } public void RemoveCommand() { SimpleCommandManager.RemoveRaiseCanExecuteChangedAction(RaiseCanExecuteChangedAction); } bool ICommand.CanExecute(object? parameter) { return canExecute((T)parameter); } public void Execute(object? parameter) { if (CanExecute(parameter)) { execute((T)parameter); } } public bool CanExecute(object? parameter) { return canExecute == null ? true : canExecute((T)parameter); } public void RaiseCanExecuteChanged() { var handler = CanExecuteChanged; if (handler != null) { handler(this, new EventArgs()); } } private readonly Action RaiseCanExecuteChangedAction;