Komenda w menu i na przycisku

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

MenuItem też ma Command. Pozycja menu i przycisk z tą samą komendą działają tak samo i razem się wyłączają, gdy notatka jest pusta. W Visual Studio to typowy układ okna: menu, pasek przycisków i pole tekstowe w DockPanel.

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="Notatnik" Height="240" Width="340">
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_Plik">
                <MenuItem x:Name="newItem" Header="_Nowy" Command="{Binding NewCommand}"/>
            </MenuItem>
            <MenuItem Header="_Edycja">
                <MenuItem x:Name="upperItem" Header="_Wielkie litery" Command="{Binding UpperCommand}"/>
            </MenuItem>
        </Menu>
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Margin="10,0,10,10">
            <Button x:Name="newButton" Content="Nowy" Width="80" Command="{Binding NewCommand}"/>
            <Button x:Name="upperButton" Content="WIELKIE" Width="80" Margin="6,0,0,0" Command="{Binding UpperCommand}"/>
        </StackPanel>
        <TextBox x:Name="textBox" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" Margin="10" TextWrapping="Wrap"/>
    </DockPanel>
</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 EditorModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

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

        private string text = "";

        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                OnPropertyChanged();
            }
        }

        public ICommand NewCommand { get; }
        public ICommand UpperCommand { get; }

        public EditorModel()
        {
            NewCommand = new RelayCommand(p => Text = "", p => Text.Length > 0);
            UpperCommand = new RelayCommand(p => Text = Text.ToUpper(), p => Text.Length > 0);
        }
    }

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

Ł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.