-1

I am using the following command to update the button on GUI

PropertyChanged(this, new PropertyChangedEventArgs(resetButton.IsEnabled = false));

However, I get an error `

Argument 1: cannot convert from 'bool' to 'string'

EDIT

This XAML code for the button that I want to disable when its clicked.

resetButton.IsEnabled = false is what I initially tried to use to disable it.

<Button x:Name="resetButton"
        Grid.Row="1" 
        HorizontalAlignment="Right" 
        VerticalAlignment="Bottom" 
        Width="80" 
        Content="Reset" 
        Style="{StaticResource BlueButton}"
        Click="resetButton_Click"/>
Taterhead
  • 5,763
  • 4
  • 31
  • 40
Imsa
  • 1,105
  • 2
  • 17
  • 39
  • The PropertyChangedEventArgs constructor takes a string as a parameter, where the string is the name of the property that was updated. The name should be of a property in the class that inherits INotifyPropertyChanged. and then resetButton.IsEnabled is bound to that in XAML. – Jay T Mar 25 '16 at 21:23

1 Answers1

-1

I will first discuss the mere syntactical error that the compiler is complaining about:

You invoke

new PropertyChangedEventArgs(resetButton.IsEnabled = false)

In there, the expression

resetButton.IsEnabled = false

will evaluate to a bool. Hence, you are passing a bool to the constructor of PropertyChangedEventArgs.

However, that constructor expects a string, namely the name of the changed property.


Now, concerning the intended result:

I am using the following command to update the button on GUI

No, you are not. At least, not directly.

What you can use the call to PropertyChanged for is to fire any event handlers registered with the PropertyChanged event, which can then do whatever you want them to do, e.g. update something in the GUI.

Therefore, somewhere else in the code, you have to write something like this:

myObject.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
{
    resetButton.IsEnabled = false;
}

This creates an anonymous method and registers it as an event handler with your event.

Now, usually, you will also want to check the PropertyName property on the e argument to see which property of myObject has actually been changed.

And here, we get back to your firing of the event: You have to pass in the property name of the changed property upon constructing the PropertyChangedEventArgs instance.

If your property is of type bool (or if you have an appropriate value converter to convert whatever type your property is to bool), you can also directly bind the resetButton.IsEnabled property in XAML to your property. It will then automatically register a handler with the PropertyChanged event under the hood in order to update the enabled state of the button when required.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
  • thank you for the detailed explanation which makes sense. However, I am new to XAML, cant figure out the whole syntax. I'd appreciate if you could put it as one piece. – Imsa Mar 25 '16 at 21:57
  • I considered adding a XAML fragment that shows the button with the binding, too ... but for that, I'd need to know what's your property (that you are firing `PropertyChanged` for) is called. – O. R. Mapper Mar 25 '16 at 22:06
  • The question still does not contain any hint as to how the property that you are binding to is named. You do have a property on your object from where you are firing the `PropertyChanged` event, right? – O. R. Mapper Mar 25 '16 at 23:03
  • To whoever downvoted this answer, may I ask what could be improved about it? – O. R. Mapper Mar 26 '16 at 13:25
  • I am new to XAML/C#, so I am not quite sure if I understand it fully. Could you please give an example about that property or hint that I can look for. Appreciate it. – Imsa Apr 08 '16 at 17:21
  • @Imsa: About which property? I think some more of your code is required to provide some specific help beyond showing a generic example for a property binding or of using `INotifyPropertyChanged` (of which there are plenty online). – O. R. Mapper Apr 09 '16 at 11:41
  • You should avoid `+=` events here. The proper route is to use either Command-Binding with a CanExecute member or bind in MVVM style to a CanReset boolean property. – H H Apr 09 '16 at 11:45
  • @HenkHolterman: Why? What is wrong about registering with an event via `+=`? Also, note that the binding route is already described in the final paragraph of the answer. – O. R. Mapper Apr 09 '16 at 11:48