You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we know, RelayCommand and getter-only properties need to be notified when their dependencies change. That is usually done with NotifyCanExecuteChangedForAttribute and NotifyPropertyChangedForAttribute.
But what needs to be done in cases when we access nested object fields, and not primitive types? I know that those classes also need to implement INotifyPropertyChanged. But that is not enough here, because that event isn't propagated to cause reevaluation of CanExecute method.
Example
There is an object of Bill.Item class that implements INotifyPropertyChanged (I have tried to use both ObservableObjectand Fody.PropertyChange, but there is no difference).
But dependent property won't be recalculated when user types in Entry fields, same thing with enabling command.
I tried to find an attribute that will help for this case, but I don't think there is a solution in this library for the given case.
API breakdown
First possibility
One way I can think of to deal with this problem would be to add a parameter to the existing ObservableProperty attribute, so that changing the field of nested ObservableObject behaves the same as changing the reference itself (which is the only thing that is tracked at the moment).
I have found a workaround, by subscribing to PropertyChanged event and manually invoking OnPropertyChanged and NotifyCanExecuteChanged. Unfortunately, that solution produces COMException and TargetInvocationException from time to time when invoked outside main UI thread. And except from that issue, I assume that this solution is not idiomatic enough because it makes code more complex.
partialvoidOnBillItemChanged(Bill.Item?oldValue, Bill.Item newValue){voidOnBillItemPropertyChanged(object?sender,PropertyChangedEventArgsargs){try{
OnPropertyChanged(nameof(NotEnoughDrinks));
SaveCommand.NotifyCanExecuteChanged();}catch(Exception){}}if(oldValue is not null)
oldValue.PropertyChanged -=OnBillItemPropertyChanged;if(newValue is not null)
newValue.PropertyChanged +=OnBillItemPropertyChanged;}
Additional context
No response
Help us help you
Yes, but only if others can assist
The text was updated successfully, but these errors were encountered:
djordje200179
changed the title
Updating properties and enabling commands that depend on observable model fields
Updating properties and enabling commands that depend on observable model properties
Jul 23, 2024
Overview
As we know,
RelayCommand
and getter-only properties need to be notified when their dependencies change. That is usually done withNotifyCanExecuteChangedForAttribute
andNotifyPropertyChangedForAttribute
.But what needs to be done in cases when we access nested object fields, and not primitive types? I know that those classes also need to implement
INotifyPropertyChanged
. But that is not enough here, because that event isn't propagated to cause reevaluation ofCanExecute
method.Example
There is an object of
Bill.Item
class that implementsINotifyPropertyChanged
(I have tried to use bothObservableObject
and Fody.PropertyChange, but there is no difference).Its properties are updated through UI with XAML two-way bindings:
SaveCommand
andNotEnoughDrinks
property depend on fields insideBillItem
.But dependent property won't be recalculated when user types in Entry fields, same thing with enabling command.
I tried to find an attribute that will help for this case, but I don't think there is a solution in this library for the given case.
API breakdown
First possibility
One way I can think of to deal with this problem would be to add a parameter to the existing
ObservableProperty
attribute, so that changing the field of nestedObservableObject
behaves the same as changing the reference itself (which is the only thing that is tracked at the moment).Second possibility
Second thing that comes to my mind is to somehow extend
NotifyCanExecuteChangedForAttribute
andNotifyPropertyChangedForAttribute
.Usage example
Breaking change?
No
Alternatives
I have found a workaround, by subscribing to
PropertyChanged
event and manually invokingOnPropertyChanged
andNotifyCanExecuteChanged
. Unfortunately, that solution producesCOMException
andTargetInvocationException
from time to time when invoked outside main UI thread. And except from that issue, I assume that this solution is not idiomatic enough because it makes code more complex.Additional context
No response
Help us help you
Yes, but only if others can assist
The text was updated successfully, but these errors were encountered: