RadioButton binding behavior; getters not always called
Posted: 27 Aug 2024, 16:28
Scenario: 2 RadioButtons, each bound to a different view model property. The RadioButtons are in an edit panel control. The control is Collapsed when not in use, and when it is needed the DataContext for the control is set to a new view model object and then the control is made Visible.
There are 2 apps, the real one and a repro app to try to repro the problem (which I could not).
Repro app:
* Running in Blend: the getter is called for each bound property once after setting the DataContext, and this behavior is consistent. As you change from one edit control display to another the getters are always called once, and are consistently called no matter how many times the controls are displayed.
* Running in Unity: the getter is called twice (at least) for each bound property after setting the DataContext, and this behavior is also consistent when changing the displayed controls.[/list]
Real app:
* Running in Blend: same behavior as the repro app; getter is called once consistently.
* Running in Unity: this is where things go off the rails, and is why I created the repro app which does not repro the behavior that I see in the real app. When displaying the control the first time, it behaves as expected and the getters are both called (twice, but that's consistent with the repro app). After that, however, only the getter for the first declared radio button gets called (also twice) when the DataContext is changed. I can swap the radio buttons and the same thing happens. If I declare a radio group then at some point neither getter is called when the DataContext is changed.
There is not a problem in detecting that the DataContext changed because there is also a string property on the DataContext and the displayed value changes correctly.
Since I can't repro this it's not a bug, but if anyone has any clue as to what's potentially going on I'd love to hear it.
Notes:
1. The app is more complex than described, but the repro app replicates the complexity.
2. I noticed the behavior first when using an IValueConverter, and switched to using properties while trying to figure out what was/wasn't happening.
There are 2 apps, the real one and a repro app to try to repro the problem (which I could not).
Repro app:
* Running in Blend: the getter is called for each bound property once after setting the DataContext, and this behavior is consistent. As you change from one edit control display to another the getters are always called once, and are consistently called no matter how many times the controls are displayed.
* Running in Unity: the getter is called twice (at least) for each bound property after setting the DataContext, and this behavior is also consistent when changing the displayed controls.[/list]
Real app:
* Running in Blend: same behavior as the repro app; getter is called once consistently.
* Running in Unity: this is where things go off the rails, and is why I created the repro app which does not repro the behavior that I see in the real app. When displaying the control the first time, it behaves as expected and the getters are both called (twice, but that's consistent with the repro app). After that, however, only the getter for the first declared radio button gets called (also twice) when the DataContext is changed. I can swap the radio buttons and the same thing happens. If I declare a radio group then at some point neither getter is called when the DataContext is changed.
There is not a problem in detecting that the DataContext changed because there is also a string property on the DataContext and the displayed value changes correctly.
Since I can't repro this it's not a bug, but if anyone has any clue as to what's potentially going on I'd love to hear it.
Notes:
1. The app is more complex than described, but the repro app replicates the complexity.
2. I noticed the behavior first when using an IValueConverter, and switched to using properties while trying to figure out what was/wasn't happening.