.NET MAUI MVVM OneWay bindings issues
See original GitHub issueThis issue has been moved from a ticket on Developer Community.
[severity:It’s more difficult to complete my work] Hi, I’m trying to use Entry and Picker components, bindings theirs Text and SelectedItem properties by OneWay mode, but it’s not working as I expect and I don’t know where I’m wrong.
I implement INotifyPropertyChanged in my viewmodel and every set methods call OnPropertyChanged(). Every getter returns a default value.
I have issues on both Picker and Entry. I made a sample project: https://1drv.ms/u/s!AtCYAZm–H6He5AKdztOYsoDu7M?e=sLFUkb
Picker:
At startup the picker has its ItemSource populated but doesn’t show any selected value.
According to documentation: A Picker can be initialized to display a specific item by setting the SelectedIndex or SelectedItem properties. However, these properties must be set after initializing the ItemsSource collection.
By breakpoint check: SelectedItem’s getter is never called!
If I define picker swapping ItemsSource with SelectedItem (just trying):
Both ItemsSource and SelectedItem getters are called (same order of definition) but still no selected value.
Pressing the button I set the SelectedItem to “Item3”. Setter is called, OnPropertyChanged() called, still no selected value on gui.
Entry:
On Entry the text is set correctly at startup.
Pressing the button I change the text, and it works.
But, if I change the text manually on the entry before pressing the button: Setter is called, OnPropertyChanged() called, but printed value doesn’t change.
I know that my property hasn’t changed since is oneway, but if I comment the value check, the OnPropertyChanged() is called and should invoke gui update anyway (in my mind).
//if (entryText != value)
//{
entryText = value;
OnPropertyChanged();
//}
It seems that there’s a value-check mechanism behind the scenes.
I made a WPF sample app (using a textbox and a combobox) that works the same way and I don’t have these issues.
Any kind of help is really appreciate as I don’t know where I’m wrong.
Thank you in advanced.
Original Comments
Feedback Bot on 4/24/2022, 01:05 AM:
(private comment, text removed)
Original Solutions
(no solutions)
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:9
Is nobody at MS seeing this? Even the most basic test cases should catch this issue, no? This is FUNDAMENTAL to all projects using Picker. SelectedItem simply will not bind to ViewModel. I tried twoway mode as well, setting the value in the ViewModel property… the breakpoint hits in the ViewModel getter (at the time the xaml is rendering), but the xaml will NOT render the selected item.
I cannot believe the web is not flooded with this issue on .NET Maui. Seems not ready for prime time. Geez. Stuff that has worked since the early Xamarin days… but not this, not here.
I know picker has a two way default binding, but this issue is about one way. One way binding is documented and should work like in the previous frameworks. In particular I’m trying to port a running wpf app on Maui. This app works in a way every control has a one way binding. The user manually enters the data, the app listens for value changes, a validation and normalization pipeline does some logics through several stagings, affecting other parameters, at the end data is set from code behind to properties updating the controls.
I made a sample Wpf app where one way binding works on the equivalent components of maui: TextBox / Entry, ComboBox / Picker - https://1drv.ms/u/s!AtCYAZm--H6HgQJMIrJ7f8aZcKxy?e=Wy7o8r
I also tested my maui code with you hints, I moved all bindings set in my code behind.
The picker now shows the correct selected item, but pressing the button value won’t change. Manual edits of Entry text make text binding hangs, like before the test.