CanExecute wyłącza przycisk
Komendy: ICommand i model widoku - Aplikacje desktopowe: WPF krok po kroku (wpf)
Drugi argument
RelayCommand to warunek. Dodaj działa tylko, gdy pole nie jest puste, a Usuń ostatni tylko, gdy lista coś ma. Przyciski same szarzeją i wracają: nie trzeba pisać addButton.IsEnabled = ... w obsłudze każdego zdarzenia. Pole ma UpdateSourceTrigger=PropertyChanged, więc model zna tekst od pierwszej litery.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="Zakupy" Height="220" Width="340">
<StackPanel Margin="10">
<StackPanel Orientation="Horizontal">
<TextBox x:Name="itemBox" Text="{Binding Item, UpdateSourceTrigger=PropertyChanged}" Width="130"/>
<Button x:Name="addButton" Content="Dodaj" Margin="6,0,0,0" Command="{Binding AddCommand}"/>
<Button x:Name="removeButton" Content="Usuń ostatni" Margin="6,0,0,0" Command="{Binding RemoveCommand}"/>
</StackPanel>
<ListBox x:Name="itemList" ItemsSource="{Binding Items}" Height="90" Margin="0,8,0,0"/>
</StackPanel>
</Window>MainWindow.xaml.cs
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using System.Collections.ObjectModel;
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 ShoppingModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private string item = "";
public string Item
{
get { return item; }
set
{
item = value;
OnPropertyChanged();
}
}
public ObservableCollection<string> Items { get; } = new ObservableCollection<string>();
public ICommand AddCommand { get; }
public ICommand RemoveCommand { get; }
public ShoppingModel()
{
AddCommand = new RelayCommand(p =>
{
Items.Add(Item.Trim());
Item = "";
}, p => Item.Trim().Length > 0);
RemoveCommand = new RelayCommand(p => Items.RemoveAt(Items.Count - 1), p => Items.Count > 0);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ShoppingModel();
}
}
}Ł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.