Skip to content

Commit

Permalink
Using the ICommand.CanExecute to enable/disable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ousiax committed Dec 11, 2023
1 parent ee8bbd9 commit 159c710
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
37 changes: 29 additions & 8 deletions src/Leo.Wpf.App/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Leo.Wpf.App.ViewModels
{
public sealed partial class MainWindowViewModel : ObservableRecipient, IDisposable
{
[ObservableProperty]
private CustomerViewModel? _currentCustomer;

private bool disposedValue;
Expand All @@ -22,6 +21,21 @@ public sealed partial class MainWindowViewModel : ObservableRecipient, IDisposab
private readonly INewCustomerDetailWindowService _newCustomerDetailWindow;
private readonly IFindWindowService _findWindow;

public CustomerViewModel? CurrentCustomer
{
get { return _currentCustomer; }
set
{
SetProperty(ref _currentCustomer, value);
NewCustomerDetailCommand.NotifyCanExecuteChanged();
EditCustomerCommand.NotifyCanExecuteChanged();
}
}

public IRelayCommand EditCustomerCommand { get; }

public IRelayCommand NewCustomerDetailCommand { get; }

public MainWindowViewModel(
ICustomerService customerService,
ICustomerDetailService detailService,
Expand All @@ -40,6 +54,12 @@ public MainWindowViewModel(
_newCustomerDetailWindow = newCustomerDetailWindowService;
_findWindow = findWindowService;

EditCustomerCommand = new RelayCommand<CustomerViewModel>(EditCustomer, c => c != null);
NewCustomerDetailCommand = new RelayCommand<string>(NewCustomerDetail, _ =>
{
return CurrentCustomer != null;
});

Messenger.Register<CustomerCreatedMessage>(this, (rcpt, msg) =>
{
Task.Run(() => ReloadCurrentCustomerAsync(msg.Id));
Expand All @@ -62,19 +82,20 @@ private void NewCustomer()
_newCustomerWindow.ShowDialog();
}

[RelayCommand]
private void UpdateCustomer()
private void EditCustomer(CustomerViewModel? customer)
{
if (CurrentCustomer != null)
if (customer != null)
{
_customerEditorWindow.ShowDialog(CurrentCustomer.Id!);
_customerEditorWindow.ShowDialog(customer.Id!);
}
}

[RelayCommand]
private void NewCustomerDetail(string customerId)
private void NewCustomerDetail(string? customerId)
{
_newCustomerDetailWindow.ShowDialog(customerId);
if (customerId != null)
{
_newCustomerDetailWindow.ShowDialog(customerId);
}
}

[RelayCommand]
Expand Down
3 changes: 2 additions & 1 deletion src/Leo.Wpf.App/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
<Button Content="{StaticResource MainWindow.ToolBar.Button.New.Content}"
Command="{Binding NewCustomerCommand}"/>
<Button Content="{StaticResource MainWindow.ToolBar.Button.Edit.Content}"
Command="{Binding UpdateCustomerCommand}" />
Command="{Binding EditCustomerCommand}"
CommandParameter="{Binding CurrentCustomer}"/>
<Button Content="{StaticResource MainWindow.ToolBar.Button.Find.Content}"
Command="{Binding FindCustomerCommand}" />
<Button Content="{StaticResource MainWindow.ToolBar.Button.Echo.Content}" />
Expand Down

0 comments on commit 159c710

Please sign in to comment.