简体   繁体   English

尽管使用SelectedItem和ItemsSource绑定,Combobox仍未更新

[英]Combobox Not Updating Despite SelectedItem and ItemsSource Bindings

I have the following ComboBox: 我有以下ComboBox:

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

It is bound to the following values in my VM: 它绑定到我的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 . 我在VM的ctor中设置了SelectedTheme的值,并且在将VM实例分配给我的PageDataContext后, get成员被点击。 My trouble is the UI does not reflect the binding value the first time I load the page; 我的麻烦是我第一次加载页面时UI没有反映绑定值; 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. 在解决了这个问题大约两个小时后,我意识到UWP框架按照它们设置的顺序连接绑定,因此正确设置了SelectedItem ,但是在设置ItemsSource值时清除了它。 Changing my XAML to the following fixes the problem: 将我的XAML更改为以下内容可解决此问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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