Fixes and mapping
This commit is contained in:
parent
fc7175c2f7
commit
ba965e8266
10 changed files with 449 additions and 284 deletions
53
imagecatalog/Commands/ButtonCommandBinder.cs
Normal file
53
imagecatalog/Commands/ButtonCommandBinder.cs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
using System;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace ImageCatalog_2.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper class to bind WinForms Button controls to ICommand implementations
|
||||
/// </summary>
|
||||
public static class ButtonCommandBinder
|
||||
{
|
||||
/// <summary>
|
||||
/// Binds a Button's Click event to an ICommand
|
||||
/// </summary>
|
||||
/// <param name="button">The button to bind</param>
|
||||
/// <param name="command">The command to execute</param>
|
||||
/// <param name="commandParameter">Optional parameter to pass to the command</param>
|
||||
public static void BindCommand(this Button button, ICommand command, object commandParameter = null)
|
||||
{
|
||||
if (button == null) throw new ArgumentNullException(nameof(button));
|
||||
if (command == null) throw new ArgumentNullException(nameof(command));
|
||||
|
||||
// Wire up the Click event to execute the command
|
||||
button.Click += (sender, e) =>
|
||||
{
|
||||
if (command.CanExecute(commandParameter))
|
||||
{
|
||||
command.Execute(commandParameter);
|
||||
}
|
||||
};
|
||||
|
||||
// Wire up CanExecuteChanged to enable/disable the button
|
||||
command.CanExecuteChanged += (sender, e) =>
|
||||
{
|
||||
button.Enabled = command.CanExecute(commandParameter);
|
||||
};
|
||||
|
||||
// Set initial enabled state
|
||||
button.Enabled = command.CanExecute(commandParameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Binds multiple buttons to commands at once
|
||||
/// </summary>
|
||||
public static void BindCommands(params (Button button, ICommand command, object parameter)[] bindings)
|
||||
{
|
||||
foreach (var (button, command, parameter) in bindings)
|
||||
{
|
||||
button.BindCommand(command, parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue