I have global collections and variables that I use throughout my app, many of the screens manipulate the content and values of these. The OnChange property for dropdowns(with this collection as its items property or default) also set global variables, I do this so that screens across the app have the same default value when entering. The problem is that changes to the dropdowns Items or Default property triggers the OnChange, even for controls outside the apps active screen. This then cascades through my app and causes somewhat confusing results. This has not been much of an issue for me until recently.
It seems that I have not understood the documentation correctly, when expecting OnChange to trigger when the users changes the control:
OnChange – How the app responds when the user changes the value of a control (for example, by adjusting a slider).
Either this is a bug that has been introduced in recent updates, or the documentation is unclear. But if the documentation means potentially any control instead of the control in "..user changes the value of a control ", it is pretty problematic in my opinion.
Anyone else experience similar problems or better ways to work around this?
Or if this is not a bug and the documentation is correct, can shed some light on my misconceptions to help me understand it better?
Assuming and hoping this is a bug, my workaround for this is wrapping a condition that checks if the active screen is the same as the controls parent screen, around the OnChange actions:
If(App.ActiveScreen.Name = Parent.Name, Action1..; Action2..);
But I fear that this yet another workaround which is going to cause problems in future updates
I tried this trick with DisplayMode, but it seems that OnChange triggers regardless of DisplayMode.
If(App.ActiveScreen.Name = Parent.Name, DisplayMode.Edit, DisplayMode.Disabled)
It is possible this is is the case for other controls, but dropdown is the only one I have tested so far.
But if you change the value of default programmatically by changing something on another screen, it is still you as the user making a change. So the OnChange event fires. Its not a bug.
Hm this is confusing to me @Pstork1
Don't you think the documentation should mention this if this is the case?
"the user changes the value of a control " To me that implies the value selected by the, user not that the state of the control itself has changed.
I did another experiment that now behaves as expected:
I have several dropdowns that change their own default variable with some pointless calculation:
Value(Text(Self.Selected.Value - 1))
Their items is is a set of numbers:
Each OnChange also adds an item to a collection
Why is only the one I change triggered this time?
The point is that you selected to change a control. The fact that the change cascaded down to the drop down and changed its state doesn't change the fact that the change was originally initiated by you. I think you are misreading the documentation. But I don't see any point in arguing about it. I can't change the documentation or the behavior and neither can you.
I'm sorry if you thought I was arguing @Pstork1. I'm only trying to understand this to resolve some issues I'm having in the best way possible. Your initial reply did not fit with the outcome of my experiment, so I just wanted to understand why 🙂 That the state cascaded is not unexpected, but OnChange did not trigger in the experiment the way it does in my app (the issue I explained in the initial post)
It does not seem to always be the case that OnChange is triggered by changing the control's default variable. There must be another factor in place for this to happen. If this is not due to some bug and I knew that factor, I would be able to resolve my issue.
We may not be able to change the documentation ourselves, but we may influence it by providing feedback 🙂
Check out our new profile badges recognizing authored solutions!
We are excited to announce the Power Apps Super Users!
Did you miss the call? Check out the Power Apps Community Call here.
Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!