question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET MAUI MVVM OneWay bindings issues

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

4reactions
dapugcommented, Mar 2, 2023

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.

1reaction
fasignocommented, Dec 14, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data binding and MVVM - .NET MAUI
The binding on each Label is the default OneWay . It only needs to display the value. However, the default binding on each...
Read more >
c# - .NET MAUI Binding Issue
ObservableObject from the MVVM Community Toolkit. Otherwise, no PropertyChanged events are raised and thus no property changes are propagated ...
Read more >
XAML Data Binding and MVVM Basics (.NET MAUI ... - YouTube
Today, we attempt to fix up some code that was submitted by a viewer. They were having issues with some data binding in...
Read more >
NET MAUI Data Binding with MVVM & XAML [5 of 8] - YouTube
After working with UI Frameworks like Flutter, VueJS, and React the MAUI DATA- Binding seems way too complicated. Too many lines of code...
Read more >
Getting Started With Basic Bindings in .NET MAUI
TwoWay is when data bindings are used with Model-View-ViewModel (MVVM). The ViewModel class is the data binding source and the view—these have ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found