I have the following ComboBox:
<ComboBox SelectedItem="{Binding SelectedTheme, Mode=TwoWay}"
ItemsSource="{Binding Themes, Mode=OneTime}" />
It is bound to the following values in my VM:
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 `SelectedTheme`s value in the VM's ctor, and the `get` member is being hit after I assign the VM instance to my `Page`'s `DataContext`. My trouble is the UI does not reflect the binding value the first time I load the page; it updates works correctly all other times, but the combobox does not show any selection after the page is initially loaded.
After struggling with this issue for about two hours, I realized that the UWP framework is connecting the bindings in the order they are set, so the `SelectedItem` is being set correctly, but is then cleared when the `ItemsSource` value is set. Changing my XAML to the following fixes the problem:
<ComboBox ItemsSource="{Binding Themes, Mode=OneTime}"
SelectedItem="{Binding SelectedTheme, Mode=TwoWay}" />