ObservesProperty CommandParameter binding bug
Binding the CommandParameter to same property passed to ObservesProperty causes wrong parameter to be passed to CanExecute because RaiseCanExecuteChanged called by the PropertyChanged event is called before the WPF binding update is called. So this causes ICommand CanExecute to receive the previous value. I think this has something to do with the order of registering to the PropertyChanged event and the subsequent invocation list.
I'm not quite sure what the best fix would be. On my application side, I simply don't bind the property to CommandParameter and my CanExecute method checks against the property directly. But, I'm sure there's a scenario out there where this isn't ideal.
I suppose you can create a new internal PropertyChangedForObservesProperty event and invoke that one after the main PropertyChangedEvent? Or maybe rearrange the invocation list the Inpc_PropertyChanged method to the bottom?
As you alluded to, I would never recommend binding to a property in your VM as a CommandParameter as there is no point or benefit in doing that.
You would have the exact same problem if you did this:
private string _raised;
public string Raised
{
get { return _raised; }
set { SetProperty(ref _raised, value); }
}
public MainWindowViewModel()
{
...
PropertyChanged += MainWindowViewModel_PropertyChanged;
}
private void MainWindowViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
ShowTestCommandRaised.RaiseCanExecuteChanged();
}
This is not a scenario that I think should be supported. It is so rare, in fact I have never heard of it before, that no time should be spent trying to figure out some way to control the timing of the INPC event and the WPF binding system. Honestly, I'm not sure how you would control the timing at all. I'm going to mark this as "won't fix" for now. Thanks for submitting this very interesting issue.
Without a straightforward way of addressing this "issue", I have to agree with your 'wontfix' assessment. I ran into this issue when I wanted to utilize the same CanExecute function for multiple commands, so I bound to the CommandParameter dependency property.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.