Files
Future/Example/MainWindow.xaml.cs
2025-08-30 17:19:57 +08:00

95 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Example
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CommandBinding cbinding = new CommandBinding(MainWindow.GetButtonContentCmd);
cbinding.Executed += Cbinding_Executed;
cbinding.CanExecute += Cbinding_CanExecute;
this.CommandBindings.Add(cbinding);
this.textCommand.OnCanExecute = (obj) => true;
this.textCommand.OnExecute += TextCommand_OnExecute;
this.butCustom.Command = this.textCommand;
}
private void TextCommand_OnExecute(object? obj)
{
MessageBox.Show("自这义命令被调用!");
}
private void Cbinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void Cbinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(e.Source.ToString());
}
public static RoutedCommand GetButtonContentCmd = new RoutedCommand();
public CustomCommand textCommand = new CustomCommand();
public class CustomCommand : ICommand
{
public string? Command { get; set; }
public CustomCommand() { }
public CustomCommand(string command, Action<object?>? onExecute = null, Func<object?, bool>? onCanExecute = null)
{
this.Command = command;
this.OnExecute = onExecute;
this.OnCanExecute = onCanExecute;
}
public event EventHandler? CanExecuteChanged;
public event Action<object?>? OnExecute;
public Func<object?, bool>? OnCanExecute;
public bool CanExecute(object? parameter)
{
if (this.OnCanExecute != null) return this.OnCanExecute(parameter);
else if (this.Execute != null) return true;
else
{
this.CanExecuteChanged?.Invoke(this, new EventArgs());
return false;
}
}
public void Execute(object? parameter)
{
this.OnExecute?.Invoke(parameter);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
new CustomWindow().Show();
}
}
}