Muhammad Touseef
Muhammad Touseef

Reputation: 4455

uwp selectedIndex binding with enum not working twoway

so I have ComboBox with some items and its SelectedIndex is bound TwoWay with a property of type MyTypeEnum the idea is its selected index value can be set by an enum to int converter and when user changes selection on combobox itself then the new selectedIndex should update the value it is bound to. it is working fine OneWay i.e: from property to SelectedIndex, but not working reverse so with breakpoints I have confirmed that Set method of my bound property does not execute when I change selection of combobox however the ConvertBack method of my converter does execute which is like it should.

I have prepared a minimal and simple code repo to reproduce the issue : https://github.com/touseefbsb/ComboBoxToEnumBug

Code

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);
    }
}

Observable

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

//using byte as parent so that I can start count from 1 instead of 0

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

Converter

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;
}

Upvotes: 1

Views: 435

Answers (1)

mm8
mm8

Reputation: 169220

I have confirmed that Set method of my bound property does not execute when I change selection of combobox however the ConvertBack method of my converter does execute which is like it should.

Your ConvertBack method returns an int but it should return an MyTypeEnum.

The source property of the view model cannot be set to an int, only to a MyTypeEnum.

Cast int to enum in C#

Upvotes: 0

Related Questions