I have a WPF page which has a DataContext assigned to it. On this page it has a couple textboxes. Each textbox is bound to a property of a parent object from the DataContext. For instance, the DataContext has a Location object on it. The Location object has properties like “Name” and “Address1” etc.
The textbox controls are bound like this:
<Binding Path="Location.Name" Mode="TwoWay">
I have a “Commit” button that only becomes enabled once all the data is valid. This relies on the evaluation of this process after a property is set. If this wasn’t a sub property of Location, I could easily do it like this:
public Location Location
{
get { return _location; }
set
{
_location = value;
OnPropertyChanged("Location");
OnPropertyChanged("IsCommitEnabled");
}
}
But since the Location object is never actually set, but rather the Location object’s “Name” property, that event never fires. Is there a way to fire off my “OnPropertyChanged(“IsCommitEnabled”)” method when a property of my Location object is modified/set?
I’m guessing your button has a Click handler and its Enabled property is bound to IsCommitEnabled. Which is fine except that you are responsible for the update of the enabled state – which is the problem you expressed.
An alternative would be to replace the Click handler and Enabled binding with a binding to a Command.
You can impliment the Command either as a RoutedCommand and set CanExecuteRoutedEventArgs.CanExecute to IsCommitEnable or you could provide an implimentation of ICommand where ICommand.CanExecute will check IsCommitEnabled.
In both these case the framework looks after polling the CanExecute method – so you dont have to do a proptry update when the property changes.
RoutedCommand example: