Problem with how OnPropertyChanged fires for WinForms app vs Xamarin app #2882
Replies: 17 comments 14 replies
-
I don't think there is an ideal solution to this discrepancy. You can change the Winforms configuration to behave like the WPF app and raise an event for every property that was changed. The downside to this is a potential performance issue because Winforms databinding reads every bound property when a single property changes. We have chosen to live with the performance hit, which is not usually noticeable. When it is noticeable due to a property that is somewhat complex to evaluate, we add an internal field to store a "cached" result, often using a nullable value. On any property change that could affect the cached result, we clear the cached field and the first property get will calculate it, but subsequent property gets will just return the cached result. |
Beta Was this translation helpful? Give feedback.
-
The only way to truly resolve this, I think, would be to create a whole new property changed infrastructure that has nothing to do with data binding, and to raise these new events on a per-property basis for every change. Windows Forms (and potentially Blazor) refresh all bindings for every WPF/UWP/Xamarin only refresh bindings on a per-property basis, based on the property name provided by each |
Beta Was this translation helpful? Give feedback.
-
I'm trying to understand what you're saying. It seems to me that for XAML, if we change Property X, and there is a dependency between Property X and Property Y, OnPropertyChanged will fire for BOTH even if Property Y doesn't actually fire. Am i correct in that assumption? Inversely, in WinForms, it will only fire for Property X. |
Beta Was this translation helpful? Give feedback.
-
We (you and I) don't control CSLA implements this interface specifically for the purpose of supporting data binding, nothing else. Any other use of the Unfortunately Microsoft hasn't been consistent with their use of
CSLA has a If you are building a WPF/UWP/Xamarin app you must use that mode, or your UI won't refresh correctly. In that mode lots of If you are building a Windows Forms app you can use either mode - but again, there's a cost to using the WPF mode because that's not how Windows Forms is designed. |
Beta Was this translation helpful? Give feedback.
-
Sorry I'm behind, but I'm trying to understand. Basically you're raising it for the dependent properties because XAML's It sounds like from a design-perspective (.NET-wise, not you), there is not a great way to handle this situation when the business objects are used for both WinForms and XAML. It seems that the "best" option would be to use XAML mode for WinForms and perhaps implement something like @hurcane suggested. |
Beta Was this translation helpful? Give feedback.
-
Close 😀 Microsoft implemented data binding differently in XAML from Windows Forms. In XAML, they require a So back in the early 2000's, before XAML, I implemented CSLA to minimize the number of Then in 2005-2006 (??) when WPF came out I had to figure out how to make CSLA work with WPF (which wants one If you use the You absolutely can always use the XAML mode (override the default |
Beta Was this translation helpful? Give feedback.
-
So let me ask this. What is the proper reason to overriding Based on your comment that "If you use the PropertyChanged event for something other than data binding, then you need to understand all these gory details, because yes, the behavior of how the event is raised is different between Windows Forms (and now Blazor) and XAML-based UI's", it sounds like doing a calculation in OnPropertyChanged would be a bad idea. |
Beta Was this translation helpful? Give feedback.
-
Ideally all business rules are implemented as business rules. Thanks to @jonnybee, the rules engine is quite robust and should meet most needs. |
Beta Was this translation helpful? Give feedback.
-
Then what would be the idea use of OnPropertyChanged on the BO side? Just trying to learn how it should be used. |
Beta Was this translation helpful? Give feedback.
-
In any ideal world you'd never use it. |
Beta Was this translation helpful? Give feedback.
-
I concur with Rocky. We have projects using Csla 3 and Csla 5. In Csla 3, we commonly used PropertyHasChanged() or OnPropertyChanged() and also event handlers within the business objects that handled the PropertyChanged event from INotifyPropertyChanged. With Csla 5, we have always been able to process internal side effects using business rules and overriding OnChildChanged when child object changes need to trigger a rule evaluation in a parent class.. |
Beta Was this translation helpful? Give feedback.
-
@hurcane, just so i'm clear, if you had to do a calculation routine to set |
Beta Was this translation helpful? Give feedback.
-
We always use AddOutValue. We never change properties through context.Target. |
Beta Was this translation helpful? Give feedback.
-
Does AddOutValue cause OnPropertyChanged to fire for it after the value changes or not? Is that what using AffectedProperties is for? For some context, when we upgraded from CSLA 3 to 5, we didn't fully accommodate the new design necessary for setting properties in business rules. |
Beta Was this translation helpful? Give feedback.
-
OnPropertyChanged behaves according the PropertyChangedMode setting. When using Winforms mode, only one PropertyChanged event is raised. Otherwise, all changed properties will raise a PropertyChanged event. The business rule execution engine and the OnChildChanged method do not care about the PropertyChangedMode. If you upgraded but did not completely rewrite your business rules, that's going to cause complications. We chose to go with a hybrid model. We forked the Csla 3 project to create a Csla3.dll with a Csla3 namespace and changed all of our legacy business projects to reference the Csla3 namespace. All new work is being done using the new paradigm with business rules. |
Beta Was this translation helpful? Give feedback.
-
I understand that. I was wondering if AddOutValue bypassed notifying property changed. It sounds like it does not. |
Beta Was this translation helpful? Give feedback.
-
I have the same situation on a WinForms app where we changed a property on a BO using a BusinessRule and the AddOutValue method to set the new value. The value is changed but PropertyChanged does not, so the UI does not gets notified. I am surely missing something but i cannot figure out what
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've run into a problem with
OnPropertyChanged
. In WinForms, it appearsOnPropertyChanged
only fires for the property that is actually changing even thoughBusinessRules
are run for any of the dependencies. In XAML,OnPropertyChanged
fires for the dependencies as well. I do know this relates to theApplicationContext.PropertyChangedMode
property.Because of this, I'm running into inconsistent behavior between our WinForms app and XAML app. Certain methods are getting called in
OnPropertyChanged
that we only expect to get called when that property value actually changes and not when it's "Changed" as a dependency, like the attached image. I tried settingPropertyChangedMode
toWindows
for the XAML app, but certain validation isn't being shown in the UI like it is when set toXAML
Do you have a suggestion on how to handle this?
Beta Was this translation helpful? Give feedback.
All reactions