-3

I have a Button bound to an ICommand interface but it isn't being fired when I run the application.

The button should be disabled when the app runs, putting a breakpoint in the ICommand or CanUpdate but it isn't being hit.

The ICommand seems to have been implemented correctly as far I can see - have substituted value in CanUpdate for simplicity...

Scratching my head to workout what is missing?....

XAML

  <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="1">                          
  <RadioButton Width="64" IsChecked="{Binding Passed}"  GroupName="T1">Yes</RadioButton> 
    
   <RadioButton Width="64" IsChecked="{Binding Passed, Converter={StaticResource InverseBoolRadioConverter}}" GroupName="T1" >No</RadioButton>
 </StackPanel >

    Button Command="{Binding UpdateHasPassed}" Content="Update"></Button>

Code-Behind:-

private RelayCommand hasPassed;

public bool Passed
{
        get
        {
            return passed;
        }
        set
        {       
            if (passed !=value )
            {
                passed = value;
                OnPropertyChanged();
            }
        }
}

public ICommand HasPassed
{
    get
    {
        if (hasPassed == null)
        {
            haspassed = new RelayCommand( param => CanUpdate());
        }

        return haspassed;
    }
}

 private bool CanUpdate()
 {
    return (1 != 2)
 }
Ram
  • 527
  • 1
  • 10
  • 26
  • `is there a way for the textblock to notify changes only if the change is different to what it was before?` do you mean original value or previous value? – Haukinger Dec 23 '20 at 18:59
  • @Haukinger if the original value was "John" and user changes it to "Jon" the button should be enabled but if the user changes it back to "John" it should become disabled again - I want functionality to check new value with original value... – Ram Dec 24 '20 at 10:20
  • Enabling and disabling a button bound to a command is automatic (given the command is implemented correctly), but noone stores the original value for you. I'd keep the whole original person struct around and compare to that (in `CanExecute` of the `ApplyCommand`) whenever a property in the view model changes. – Haukinger Dec 24 '20 at 10:53
  • @Haukinger - I have been able to make it work such that when a textbox was null but then user enters text the button is enabled but can't workout how to pass the original person struct in the CanExecute - any pointers appreciated – Ram Dec 30 '20 at 16:14
  • 1
    No need to pass it, it's a field in the class containing the command, you can just use it like any other field. – Haukinger Jan 01 '21 at 14:40

1 Answers1

0

You're on the right track looking into INotifyPropertyChanged. I would also recommend reading up on WPF data bindings, the ICommand interface (and specifically creating a RelayCommand, more on that later), and MVVM design.

The benefit of WPF data bindings and ICommand is that you can control when the button gets enabled or disabled, based on your conditional criteria (i.e. name has changed from its original value). With the tips mentioned here, you should be able to do what you want in short time. Just google each of the topics and you'll get what you need.

Tam Bui
  • 2,940
  • 2
  • 18
  • 27