Jedna komenda, wiele przycisków: CommandParameter

Komendy: ICommand i model widoku - Aplikacje desktopowe: WPF krok po kroku (wpf)

Przyciski cyfr wiążą się z tą samą komendą DigitCommand, a CommandParameter podaje cyfrę. Parametr trafia do Execute i CanExecute jako object (z XAML to napis, stąd (string)p). Po czterech cyfrach CanExecute zwraca false, więc wszystkie przyciski cyfr się wyłączają, a C czyści kod.

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PIN" Height="200" Width="240">
    <StackPanel Margin="10">
        <TextBlock x:Name="pinText" Text="{Binding Pin}" FontSize="22" Height="30"/>
        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
            <Button x:Name="digit1Button" Content="1" Width="40" Margin="0,0,4,0" Command="{Binding DigitCommand}" CommandParameter="1"/>
            <Button x:Name="digit2Button" Content="2" Width="40" Margin="0,0,4,0" Command="{Binding DigitCommand}" CommandParameter="2"/>
            <Button x:Name="digit3Button" Content="3" Width="40" Margin="0,0,4,0" Command="{Binding DigitCommand}" CommandParameter="3"/>
            <Button x:Name="digit4Button" Content="4" Width="40" Margin="0,0,4,0" Command="{Binding DigitCommand}" CommandParameter="4"/>
        </StackPanel>
        <Button x:Name="clearButton" Content="C" Width="40" HorizontalAlignment="Left" Margin="0,6,0,0" Command="{Binding ClearCommand}"/>
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApp
{
    public class RelayCommand : ICommand
    {
        private readonly Action<object> execute;
        private readonly Func<object, bool> canExecute;

        public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            return canExecute == null || canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            execute(parameter);
        }
    }

    public class PinModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        private string pin = "";

        public string Pin
        {
            get { return pin; }
            set
            {
                pin = value;
                OnPropertyChanged();
            }
        }

        public ICommand DigitCommand { get; }
        public ICommand ClearCommand { get; }

        public PinModel()
        {
            DigitCommand = new RelayCommand(p => Pin += (string)p, p => Pin.Length < 4);
            ClearCommand = new RelayCommand(p => Pin = "", p => Pin.Length > 0);
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new PinModel();
        }
    }
}

Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.

Wszystkie lekcje kursu Aplikacje desktopowe: WPF krok po kroku·Zaloguj się, aby zapisać postęp na koncie.