【问题标题】:uwp selectedIndex binding with enum not working twowayuwp selectedIndex 绑定与枚举不能双向工作
【发布时间】:2023-03-27 16:40:01
【问题描述】:

所以我有 ComboBox 和一些项目,它的 SelectedIndex 绑定 TwoWayMyTypeEnum 类型的属性这个想法是它的选定索引值可以由 enum to int 转换器设置,当用户更改组合框本身的选择时,新的 selectedIndex 应该更新它绑定到的值.它工作正常 OneWay 即:从属性到 SelectedIndex,但不工作 reverse 所以断点我已经确认我的绑定属性的 Set 方法当我更改组合框的选择时不会执行,但是我的转换器的 ConvertBack 方法确实会执行,就像它应该执行的那样。

我准备了一个最小且简单的代码库来重现该问题:https://github.com/touseefbsb/ComboBoxToEnumBug

代码

MainPage.xaml

<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
    <local:IdToIndexConverter x:Key="IdToIndexConverter"/>
</Page.Resources>
<Grid x:DefaultBindMode="TwoWay">
    <ComboBox SelectedIndex="{x:Bind ViewModel.MyTypeEnum, Converter={StaticResource IdToIndexConverter}}" >
        <ComboBoxItem>item 1</ComboBoxItem>
        <ComboBoxItem>item 2</ComboBoxItem>
        <ComboBoxItem>item 3</ComboBoxItem>
    </ComboBox>
</Grid>

MainViewModel

public class MainViewModel : Observable
{
    private MyTypeEnum _myTypeEnum = MyTypeEnum.Type1;

    public MyTypeEnum MyTypeEnum
    {
        get => _myTypeEnum;
        set => Set(ref _myTypeEnum, value);
    }
}

可观察

public class Observable : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
    {
        if (Equals(storage, value))
        {
            return;
        }

        storage = value;
        OnPropertyChanged(propertyName);
    }

    protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

MyTypeEnum

//使用 byte 作为父级,这样我就可以从 1 而不是 0 开始计数

public enum MyTypeEnum : byte
{
    Type1 = 1,
    Type2,
    Type3
}

转换器

public class IdToIndexConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language) => System.Convert.ToInt32(value) - 1;

    public object ConvertBack(object value, Type targetType, object parameter, string language) => ((int)value) + 1;
}

【问题讨论】:

    标签: c# xaml uwp combobox ivalueconverter


    【解决方案1】:

    我已经确认,当我更改组合框的选择时,我的绑定属性的 Set 方法不会执行,但是我的转换器的 ConvertBack 方法确实会执行,就像它应该执行的那样。

    您的ConvertBack 方法返回一个int,但它应该返回一个MyTypeEnum

    视图模型的源属性不能设置为int,只能设置为MyTypeEnum

    Cast int to enum in C#

    【讨论】:

    • 我也怀疑过,但这不是问题,因为当发生这种情况时,set 方法通常会给出强制转换异常,但在我的情况下,set 方法永远不会触发。
    • “设置方法”?不,如果类型不匹配,这甚至不会被调用。你试过我的建议了吗?
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    相关资源
    最近更新 更多