The PropertyChangedCallback on a DependecyProperty is never called on NotifyPropertyChangedEvents for certain properties

The user interface of RemoteX Applications has a concept of linking objects to each other. This is described in the UI very similar to a link on a webpage. Some of these links can be changed using an autocomplete like scenario.

These links have had us create user controls to handle them. We’re doing some changes to the interaction to these controls as such we had an issue where the controls never updated their content. This was due to the PropertyChangedCallback was never fired on their dependency property.

The backing field for a link looks something similar to this:

public IRef Contract {
	get { return _contract; }
    set {
    	_contract.Href = value.Href;
	    UpdateContract();
    	OnPropertyChanged( Properties.Contract );
    }
}

As you can see the actual object is never changed, we just change the link on the object.

Apparently WPF optimizes its bindings using ReferenceEquals. The PropertyChangedCallback event is never fired if the bound property is still bound to the same object. so the user control never gets the changed object. This can cause problems for other scenarios as well.

The solution for us was to always return a new copy of the link. That way the user control’s PropertyChangedCallback is always fired. Example below:

public IRef Contract {
	get { 
    		//We need to return a new instance of an IRef here since WPF does ReferenceEquals to check for changes
	        return new Ref<IContract>(_contract);
        }
    set {
    		_contract.Href = value.Href;
		    UpdateContract();
	    	OnPropertyChanged( Properties.Contract );
    }
}