简体   繁体   中英

Combobox Not Updating Despite SelectedItem and ItemsSource Bindings

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}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM