Combobox Not Updating Although SelectedItem and ItemsSource Bindings

I have the following ComboBox:

<ComboBox SelectedItem="{Binding SelectedTheme, Mode=TwoWay}" ItemsSource="{Binding Themes, Mode=OneTime}" /> 

It is associated with the following values ​​in my virtual machine:

 private Theme _selectedTheme; public Theme SelectedTheme { get { return _selectedTheme; } set { if (_selectedTheme != value) { _selectedTheme = value; OnPropertyChanged(); } } } public List<Theme> Themes => Enum.GetValues(typeof(Theme)).Cast<Theme>().ToList(); 

I set the SelectedTheme value to VM ctor, and the get element gets after I assigned the VM instance to my Page DataContext . My problem is that the user interface does not reflect the value of the binding on the first page load; it updates correctly all the other time, but the combobox does not show any choice after the page loads.

+5
source share
1 answer

After dealing with this problem for about two hours, I realized that the UWP structure connects the bindings in the order in which they are set, so the SelectedItem parameter is set correctly, but then cleared when the ItemsSource value is set. Changing my XAML to the following fix for the problem:

 <ComboBox ItemsSource="{Binding Themes, Mode=OneTime}" SelectedItem="{Binding SelectedTheme, Mode=TwoWay}" /> 
+6
source

Source: https://habr.com/ru/post/1261890/


All Articles