blah blah blah is here! blah blah » Close

up1down
link

I have a button. I want to bind the IsEnabled property to a boolean value. I tried something like this...

<Button Name="btnUpdate" Content="Update" Click="btnUpdate_Click" TabIndex="3" IsEnabled="{Binding UpdateAvailable}" ></Button>


However the state does not seem to follow that of the UpdateAvailable property. Thoughts as to where I am going wrong?

--- EDIT ---



public event PropertyChangedEventHandler PropertyChanged;

protected virtual void NotifyPropertyChanged(object sender, string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}






private bool updateAvailable;
[XmlIgnore]
public bool UpdateAvailable { get { return updateAvailable; } set { this.updateAvailable = value; NotifyPropertyChanged(this, "UpdateAvailable"); } }

last answered one year ago

1 answers

link

Three possibilities occur to me:

1. The UpdateAvailable property is not public.

2. The DataContext property of btnUpdate or its containing Grid or Window hasn't been set to an instance of whatever class contains the UpdateAvailable property.

3. The class referred to in 2. above doesn't implement INotifyPropertyChanged.

eeboy
499

Hmmm... check, check and check. Still no joy!

eeboy
499

Interesting. I can initialize the value in the constructor of the class which contains UpdateAvailable and the button IsEnabled property follows that upon startup. However, whenever UpdateAvailable changes after that point the IsEnabled property does not follow. That seems to point to my NotifyPropertyChanged call eh?

eeboy
499

Argggg.... I found it! I had everything correct but the class containing UpdateAvailable did not implement (public class Device : INotifyPropertyChanged) the IPropertyChangeNotification. Of course nobody would have noticed that because the entire class was not posted in my original post. I am still quite new to .Net and C# so... how could I have prevented this? Could the compiler have told me something was funny since I am using the PropertyChangedEventHandler in a class that doesn't implement IPropertyChangedNotification?

vulpes
17279

Unfortunately, there is no way that the compiler could pick up a mistake like that. For all it knows a class which doesn't implement INotifyPropertyChanged could still have an event called PropertyChanged of type PropertyChangedEventHandler. There might be scope for an advanced debugging tool to sniff out such errors though I don't know of one.

Feedback