ValueTuple does not support DisplayMemberPath. Combobox, WPF

Viewed 2144

I have a combobox and want to bind its ItemsSource to an IEnumerable<(string,string)>. If I do not set DisplayMemberPath, then it works and it shows in the dropdown area the result of calling ToString() in the items. Nevertheless when I set DisplayMemberPath="Item1" it does not show anything anymore. I have made the following sample in which you may see that if I use classic Tuple type it works as expected.

When debugging I have checked that the valuetuple has Item1 and Item2 as properties also.

My XAML:

<Window x:Class="TupleBindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Loaded="MainWindow_OnLoaded"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <ComboBox x:Name="TupleCombo" Grid.Row="0" VerticalAlignment="Center" 
                  DisplayMemberPath="Item1" />
        <ComboBox x:Name="ValueTupleCombo" Grid.Row="1" VerticalAlignment="Center" 
                  DisplayMemberPath="Item1" />
    </Grid>
</Window>

And my codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace TupleBindingTest
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private IEnumerable<Tuple<string, string>> GetTupleData()
        {
            yield return Tuple.Create("displayItem1", "valueItem1");
            yield return Tuple.Create("displayItem2", "valueItem2");
            yield return Tuple.Create("displayItem3", "valueItem3");
        }

        private IEnumerable<(string, string)> GetValueTupleData()
        {
            yield return ( "displayItem1", "valueItem1");
            yield return ("displayItem2", "valueItem2");
            yield return ("displayItem3", "valueItem3");
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            TupleCombo.ItemsSource = GetTupleData();
            ValueTupleCombo.ItemsSource = GetValueTupleData();
        }
    }
}

At runtime this sample will show the data properly in the first combobox but will show nothing in the second.

Why does this happen?

1 Answers
Related