You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to clarify whether this issue is caused by coding or by a bug in .NET MAUI's Binding.
A Button and a ContentView are placed on the MainPage.
Nothing is placed in ContentView's Content until the Button is pressed.
Below is the screen layout of the MainPage.
When the Button is pressed, the control defined in Popup.xaml is placed in ContentView's Content.
Below is the internal logic of MainPage.
[MainPage.xaml.cs]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void btnShow_Clicked(object sender, EventArgs e)
{
var popup = new Popup();
var view = popup.Content;
// view.SetBinding(BindingContextProperty, new Binding { Source = popup, Path = BindingContextProperty.PropertyName });
cvContent.Content = view;
cvContent.SetBinding(BindingContextProperty, new Binding { Source = popup, Path = BindingContextProperty.PropertyName });
}
}
Below is the screen layout of the Popup that is placed when the button is pressed.
A Picker is placed in the Popup, and each property of TestViewModel is bound to ItemsSource and SelectedItem.
public partial class Popup : ContentView
{
public Popup()
{
InitializeComponent();
this.BindingContext = new ViewModelTest();
}
}
The ViewModelTest class that binds to the Picker looks like this:
In the constructor, we set the first element of Outlets to SelectedItemOutlet.
[ViewModelTest.cs]
public class ViewModelTest : INotifyPropertyChanged
{
Outlet _selectedItemOutlet;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public Outlet SelectedItemOutlet
{
get
{
return _selectedItemOutlet;
}
set
{
if (_selectedItemOutlet != value)
{
_selectedItemOutlet = value;
NotifyPropertyChanged();
}
}
}
public ObservableCollection<Outlet> Outlets { get; set; }
public ViewModelTest()
{
Outlets = new ObservableCollection<Outlet>()
{
new Outlet() { Name = "Outlet0" },
new Outlet() { Name = "Outlet1" },
new Outlet() { Name = "Outlet2" },
};
SelectedItemOutlet = Outlets[1];
}
}
public class Outlet
{
public string Name { get; set; }
}
I expected the picker to select and display the first element in the outlet when the button is pressed.
But SelectedItem was set to null.
The processing when the button is pressed is as follows.
var popup = new Popup();
var view = popup.Content;
cvContent.Content = view;
cvContent.SetBinding(BindingContextProperty, new Binding { Source = popup, Path = BindingContextProperty.PropertyName });
Create a Popup and set the Popup's Content to the ContentView's Content.
Finally, bind the BindingContextProperty to the ContentView with the Source as popup.
When I debugged it, I found that the BindingContext was initialized to null when the parent of Content was replaced.
cvContent.Content = view;
This initialization was initializing the Picker's ItemsSource to null, SelectedIndex to -1, and SelectedItem to null.
The following code propagates the BindingContext from the popup, but the Picker's SeletedItem has been initialized to null due to the previous initialization. Since the value of the ViewModel that propagates is also null, SelectedItem will not change from null.
var popup = new Popup();
var view = popup.Content;
view.SetBinding(BindingContextProperty, new Binding { Source = popup, Path = BindingContextProperty.PropertyName });
cvContent.Content = view;
I tried binding the BindingContextProperty to the Popup's Content with the Popup as the source before replacing the Content parent from the Popup to a ContentView . As a result, the Picker's BindingContext is no longer initialized to null when the parent of Content is changed from Popup to ContentView.
I would like to know if the above code is correct as a solution, if there is no problem with the code before the fix, and if there is no problem, is it a .NET MAUI bug?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to clarify whether this issue is caused by coding or by a bug in .NET MAUI's Binding.
A Button and a ContentView are placed on the MainPage.
Nothing is placed in ContentView's Content until the Button is pressed.
Below is the screen layout of the MainPage.
[MainPage.xaml]
When the Button is pressed, the control defined in Popup.xaml is placed in ContentView's Content.
Below is the internal logic of MainPage.
[MainPage.xaml.cs]
Below is the screen layout of the Popup that is placed when the button is pressed.
A Picker is placed in the Popup, and each property of TestViewModel is bound to ItemsSource and SelectedItem.
[Popup.xaml]
Below is the internal logic of the popup.
[Popup.xaml.cs]
The ViewModelTest class that binds to the Picker looks like this:
In the constructor, we set the first element of Outlets to SelectedItemOutlet.
[ViewModelTest.cs]
I expected the picker to select and display the first element in the outlet when the button is pressed.
But SelectedItem was set to null.
The processing when the button is pressed is as follows.
Create a Popup and set the Popup's Content to the ContentView's Content.
Finally, bind the BindingContextProperty to the ContentView with the Source as popup.
When I debugged it, I found that the BindingContext was initialized to null when the parent of Content was replaced.
This initialization was initializing the Picker's ItemsSource to null, SelectedIndex to -1, and SelectedItem to null.
The following code propagates the BindingContext from the popup, but the Picker's SeletedItem has been initialized to null due to the previous initialization. Since the value of the ViewModel that propagates is also null, SelectedItem will not change from null.
Therefore, I changed the source code as follows.
I tried binding the BindingContextProperty to the Popup's Content with the Popup as the source before replacing the Content parent from the Popup to a ContentView . As a result, the Picker's BindingContext is no longer initialized to null when the parent of Content is changed from Popup to ContentView.
I would like to know if the above code is correct as a solution, if there is no problem with the code before the fix, and if there is no problem, is it a .NET MAUI bug?
Below is the source code to reproduce this issue.
https://github.com/cat0363/MauiComm-ReproPopupPicker.git
I'd love to hear from anyone familiar with binding propagation. Thank you.
Beta Was this translation helpful? Give feedback.
All reactions