.NET MAUI MVVM OneWay bindings issues
This 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)
Still present in 6.0.400
No selected value, but entry works.

Once entry is edited manually, works no more. On combo still no selected value.

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.
At the time I'm writing the issue is still here, even on .net 7.0. It's 8 months I opened this issue, I know you have a lot of work, but I see issues that are tagged and discussed in short time after posted. This isn't even tagged apart from "area/Controls"! I know the content is malformed but that's caused I posted in Developer Community, see referred link.
I'm seriously considering to switch to more stable frameworks.
I found a Picker SelectedItem workaround for mac catalyst from https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/picker?view=net-maui-7.0.
A Picker can be initialized to display a specific item by setting the SelectedIndex property. However, the SelectedIndex property must be set after initializing the Items collection.
So, you can put this into constructor after InitializeComponent();:
YourPicker.ItemsSource = yourViewModel.YourList;
YourPicker.SelectedItem = yourViewModel.YourSelectedValue;
Btw, It worked on Windows as expected and without this workaround.
@dekipet
Btw, It worked on Windows as expected and without this workaround.
Did you try with one way bindings?
@fasigno No, it's not working with one way binding on Windows, but with constructor initialization above yes. Btw, the SelectedIndex and SelectedItem properties have a default binding mode of BindingMode.TwoWay for a Picker.
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.
Binding binding = new Binding("ComboItems", BindingMode.OneWay);
myPicker.SetBinding(Picker.ItemsSourceProperty, binding);
Binding selectedBinding = new Binding("PickerSelectedItem", BindingMode.OneWay);
myPicker.SetBinding(Picker.SelectedItemProperty, selectedBinding);
Binding entryBinding = new Binding("EntryText", BindingMode.OneWay);
CounterLabel.SetBinding(Entry.TextProperty, entryBinding);
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.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
LOL. @mattleibow are you sure you want to backlog that? In my days as an MS employee, I'd have it as P0 or P1 and get it into the very next release, if not an out of band hot fix!). Binding is not a "nice to have' feature, it is mission critical, and there is no solid work-around (that anyone here has come up with).