UWP ComboBox SelectedItem在弹出窗口中未按预期绑定

Flo*_*ser 5 uwp uwp-xaml

我没有使用该SelectedItem属性Combobox.项目已正确绑定并显示,但无法更改为其他项目.如果尝试选择其他项目,则项目列表将正确关闭,但SelectedItem不会被调用(也不会设置为setter或getter),并且不会更改显示的所选项目.

我的XAML如下:

<ComboBox
    ItemsSource="{Binding PasswordTypes}"
    ItemTemplate="{StaticResource PasswordTypeTemplate}"
    SelectedItem="{Binding SelectedPasswordType, Mode=TwoWay}"
    />
Run Code Online (Sandbox Code Playgroud)

相关ViewModel代码:

public MyViewModel()
{
    //these are the only two assignments in code of those two properties
    _passwordTypes = new ObservableCollection<PasswordType>(nonEmptyList);
    _selectedPasswordType = PasswordTypes.First();
}

private PasswordType _selectedPasswordType;
public PasswordType SelectedPasswordType
{
    get => _selectedPasswordType;
    set => Set(ref _selectedPasswordType, value);
}

private ObservableCollection<PasswordType> _passwordTypes;
public ObservableCollection<PasswordType> PasswordTypes
{
    get => _passwordTypes;
    set => Set(ref _passwordTypes, value);
}
Run Code Online (Sandbox Code Playgroud)

两个属性的调用如下:

  1. get PasswordTypes 起源于 this.InitializeComponent()
  2. get SelectedPasswordType 起源于 this.InitializeComponent()
  3. set SelectedPasswordType起源this.InitializeComponent()null
  4. set SelectedPasswordType源于(评估)this.InitializeComponent()的实例PasswordType_passwordTypes.Contains(value);true
  5. 之后不再对这两个属性进行调用

这就是我所看到的: ComboBox行为

我创建了一个分支,只需要我写这个问题所需的最小更改:https://github.com/famoser/Bookmarked/compare/bug-failing-combobox

如果我更换ComboBoxListViewSelectedItem设置是否正确.因此,设置正常.

我是否需要为此设置其他属性ComboBox,或者这是一个错误?

Jus*_* XL 8

它不起作用的原因是因为你ComboBox永远不会聚焦所以SelectionChanged事件永远不会被解雇.

此行为是从Windows 10 build 14393开始设计的.修复很简单 - 您只需要手动启用对您的交互的关注AppBarButton.

有一个新的名为属性AllowFocusOnInteraction在推出14393,不只是这个.因此,如果您定位到14393及更高版本,则将其设置false为您需要做的全部操作.

如果你在此之前定位任何东西,你将需要在你的情况下做以下Loaded事情AppBarButton.

private void AppBarButton_Loaded(object sender, RoutedEventArgs e)
{
    var allowFocusOnInteractionAvailable =
        Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent(
            "Windows.UI.Xaml.FrameworkElement",
            "AllowFocusOnInteraction");

    if (allowFocusOnInteractionAvailable)
    {
        if (sender is FrameworkElement s)
        {
            s.AllowFocusOnInteraction = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要了解有关此行为的更多信息,请参阅Rob Caplan的优秀帖子"附加到AppBarButton的弹出窗口上的ComboBox在1607上丢失鼠标输入".