ComboBox最初不选择绑定值 [英] ComboBox does not select binding value initially

查看:80
本文介绍了ComboBox最初不选择绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,不要重复。

TextBox和CheckBox可以正常工作,但不能正常使用ComboBox。

TextBox and CheckBox work correctly, just not ComboBox.

internal class Word : _Model
{
    public enum Categories
    {
        Noun,
        Verb,
    }

    private Categories category;
    public Categories Category
    {
        get
        {
            return this.category;
        }
        set
        {
            this.SetProperty(ref this.category,
                             value);
        }
    }
}



通知(可以安全阅读)



Notify (safe to skip reading this part)

internal abstract class _Model : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (object.Equals(storage, value))
        {
            return false;
        }
        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



控件(模型)



Control (Model)

public sealed partial class WordEditor : UserControl
{
    public WordEditor()
    {
        this.Model = new Models.Word();
        this.Model.Category = Models.Word.Categories.Verb;
        this.InitializeComponent();
    }

    private Models.Word Model { get; set; }

    internal IList<KeyValuePair<Models.Word.Categories, string>> Categories { get { return new List<KeyValuePair<Models.Word.Categories, string>>() { new KeyValuePair<Models.Word.Categories, string>(Models.Word.Categories.Noun, "Noun"), new KeyValuePair<Models.Word.Categories, string>(Models.Word.Categories.Verb, "Verb") }; } }
}



绑定



Binding

<UserControl.Resources>
    <converters:AnyConverter x:Key="AnyConverter"/>
</UserControl.Resources>

<StackPanel>
    <ComboBox ItemsSource="{x:Bind Path=Categories, Mode=OneTime}"
              DisplayMemberPath="Value"
              SelectedValuePath="Key"
              SelectedValue="{x:Bind Path=Model.Category, Mode=TwoWay, Converter={StaticResource ResourceKey=AnyConverter}}"/>
</StackPanel>



转换器



Converter

internal class AnyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value;
    }
}



问题



组合框正确显示了动词和名词,并且模型正确反映了动词或名词的选择。

Issue

The ComboBox has "Verb" and "Noun" displayed correctly, and selection on "Verb" or "Noun" is reflected by the model correctly.

但是,即使类别最初设置为动词,ComboBox也不会显示该动词。

However, even though Category is set to Verb at the very first place, the ComboBox does not show that.

如何修复它,以便ComboBox在出现以下情况时选择动词

How to fix it so that the ComboBox selects "Verb" when it first appears?

推荐答案

我转载了您的问题。这里的问题是 ComboBox.SelectedValue 不适用于枚举类型。为了测试这一点,我们可以使用不带绑定的 ComboBox 并在代码中设置 SelectedValue ,例如:

I reproduced your issue. The problem here is ComboBox.SelectedValue doesn't work with enumeration type. To test this, we can use a ComboBox without binding and set the SelectedValue in code-behind like:

<ComboBox x:Name="cmb" DisplayMemberPath="Value" SelectedValuePath="Key" />

cmb.ItemsSource = Categories;
cmb.SelectedValue = Models.Word.Categories.Verb;

尽管我已经设置了 Models.Word.Categories.Verb SelectedValue ,但其值仍为 null

Although I've set Models.Word.Categories.Verb to SelectedValue, but its value still be null.

但是,当 Value 类型是 int string 。根据我在WPF中的经验,枚举类型也应该起作用。我认为这可能是UWP中的错误。

However, it works when the Value type is int or string. From my WPF experience, enumeration type should also work. I think this may be a bug in UWP.

作为一种解决方法,我认为您可以使用 SelectedIndex SelectedItem 属性在组合框首次出现时选择动词。例如:

As a workaround, I think you can use SelectedIndex or SelectedItem property to select "Verb" when the ComboBox first appears. For example:

在XAML中,使用 SelectedIndex 代替 SelectedValue

In the XAML, use SelectedIndex instead of SelectedValue:

<ComboBox DisplayMemberPath="Value"
          ItemsSource="{x:Bind Path=Categories, Mode=OneTime}"
          SelectedIndex="{x:Bind Path=Model.Category, Mode=TwoWay, Converter={StaticResource ResourceKey=AnyConverter}}"
          SelectedValuePath="Key" />

并更改 Converter ,例如:

internal class AnyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (int)(Models.Word.Categories)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return (Models.Word.Categories)(int)value;
    }
}

此后,设置 this .Model.Category = Models.Word.Categories.Verb; 可以工作。

这篇关于ComboBox最初不选择绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆