0

I just started using and learning mvvm. A RelayCommand I set on the ViewModel side does not disable the button on the view side. When I initially set CanExecute return false the button appears to be disabled and the keyboard is not getting focus. But the button doesn't get disabled when CanExecute returns false at runtime like below. Where could the error be?

<Button Content="Test" 
        Grid.Column="1"
        Width="60" 
        Height="50" 
        Command="{Binding Path=TestCommand}"/>
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using TestWpfApplication.Models;

namespace TestWpfApplication.ViewModels
{
    public class PersonView : ObservableObject
    {
        private bool canExecute=true;
        public event PropertyChangedEventHandler PropertyChange;
        
        public bool _canExecute 
        { 
            get => canExecute; 
            set => SetProperty(ref canExecute,value); 
        } 
        
        private Person person;

        public Person Person
        {
            get => person;
            set => SetProperty(ref person, value);
        }

        public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>();

        public PersonView()
        {
                        person = new Person();
            Persons.Add(new Person() { Ad = "Fatih", Soyad = "Uyanık", Yas = 12 });
            Persons.Add(new Person() { Ad = "eymen", Soyad = "Uyanık", Yas = 4 });
            Persons.Add(new Person() { Ad = "Deneme1", Soyad = "Deneme1", Yas = 12 });
        }
        
                       private ICommand testCommand;
        public ICommand TestCommand => testCommand = new RelayCommand(Test, TestCanExecute);

        private int Sayac = 0;
        private bool TestCanExecute() => canExecute;
        private void Test()
        {
            canExecute = Sayac++<3;
                                }

    }
}
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • 2
    ICommand has an event called CanExecuteChanged. This event has to fire in order for WPF to call the CanExecute method again. RelayCommand has a public method NotifyCanExecuteChanged(). I think that's what you want to use if you want to avoid things like binding to IsEnabled on your button etc – Ready Cent May 09 '23 at 01:05
  • Hello I am trying to use it that way. But the button is not disabled at runtime. I actually wanted to use it this way without using IsEnable. However it didn't work for me. I think there is something I missed somewhere. – Fatih Uyanık May 09 '23 at 07:03
  • 1
    See here: https://stackoverflow.com/q/28343632/1136211 and https://stackoverflow.com/q/1340302/1136211 – Clemens May 09 '23 at 07:09

1 Answers1

-2

you can easily bind the IsEnabled property of the button, and by setting that property to false the button will be disabled.

<Button Content="Test" Grid.Column="1" Width="60" Height="50" Command="{Binding Path=TestCommand}" IsEnabeled="{Binding IsButtonEnabled}"/>

On the ViewModel

        bool isButtonEnabled;
    public bool IsButtonEnabled
    {
        get => isButtonEnabled;
        set => SetProperty(ref isButtonEnabled, value);
    }
PouyaF
  • 32
  • 4
  • 1
    Possible yes, but with the command binding everything is already at hand - when you use it. This is like jumping over the river when the bridge is just a step next to you ;o) – Sir Rufo May 09 '23 at 03:22