Search code examples
c#wpfmvvmbindingcanexecute

WPF MVVM CanExecute method implementation problem


I am trying to implement a WPF MVVM application. And I am a beginner in this platform. All I need is to implement a CanExecute method for my SaveCommand

Here is the code

WPF XML Code

        <Canvas Height="283" Name="canvas1" Width="321">
            <Label Canvas.Left="6" Canvas.Top="6" Content="First Name" Height="25" Width="91" />
            <Label Canvas.Left="6" Canvas.Top="37" Content="Last Name" Height="25" Width="91" />
            <TextBox Canvas.Left="103" Canvas.Top="10" Height="21"  Width="199" Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}"/>
            <TextBox Canvas.Left="103" Canvas.Top="37" Height="21"  Width="199" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}"/>
            <Button Canvas.Left="228" Canvas.Top="241" Content="Save" Height="23" Width="74" Command="{Binding SaveCommand}" />
            <TextBlock Canvas.Left="28" Canvas.Top="110" Height="19" Width="259" Text="{Binding Path=FullName}"/>
        </Canvas>

C# ViewModel code

    public CustomerViewModel(IEventAggregator eventAggregator, IUnityContainer container, ILoggerFacade logger)
        : base(eventAggregator, container, logger)
    {
        logger.Log("View Model Initialized", Category.Debug, Priority.None);
        InitializeCommands();
    }

    public DelegateCommand<object> SaveCommand { get; set; }

    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set { 
            firstName = value;
            RaisePropertyChanged("FirstName");
        }
    }

    private string lastName;

    public string LastName
    {
        get { return lastName; }
        set { 
            lastName = value;
            RaisePropertyChanged("LastName");
        }
    }
    private string fullName;

    public string FullName
    {
        get { return fullName; }
        set { 
            fullName = value;
            RaisePropertyChanged("FullName");
        }
    }


    private void InitializeCommands()
    {
        SaveCommand = new DelegateCommand<object>(OnSaveCommand, CanSaveExcute);

    }
    private bool CanSaveExcute(object obj)
    {
        if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName))
            return false;
        return true;
    }

    private void OnSaveCommand(object obj)
    {
        FullName = FirstName + " " + LastName;
    }

Without the can CanSaveExcute it works fine. After setting CanSaveExcute the button remains disable and does not enable on text change. As far as I have learned this is the way to go.

Please Show me what I am doing wrong.

Thanks


Solution

  • You should also raise the "CanExecuteChanged" event, otherwise the receiver would have to permanently revisit your command, to find out whether it can be executed. You can use the "RaiseCanExecuteChanged" method, described here

    Edit If you don't mind the overhead, you can just call this.SaveCommand.RaiseCanExecuteChanged() after your call to RaisePropertyChanged("FullName"); in the property-setters. If you RaisePropertyChanged method is virtual, you could place the RaiseCanExecuteChanged() their.