ValueTuple不支持DisplayMemberPath.Combobox,WPF

taq*_*ion 1 c# wpf c#-7.0 valuetuple

我有一个组合框,并希望将其ItemsSource绑定到IEnumerable<(string,string)>.如果我没有设置DisplayMemberPath,那么它可以工作,它在下拉区域显示调用ToString()项目的结果.然而,当我设置DisplayMemberPath="Item1"它不再显示任何东西.我做了以下示例,您可能会看到如果我使用经典Tuple类型,它会按预期工作.

调试时我已经检查过valuetuple还有Item1和Item2作为属性.

我的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>
Run Code Online (Sandbox Code Playgroud)

我的代码隐藏:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在运行时,此示例将在第一个组合框中正确显示数据,但在第二个组合框中不显示任何内容.

为什么会这样?

Grx*_*x70 8

那是因为DisplayMemberPath内部为每个项目的模板设置了与指定路径的绑定.所以设置DisplayMemberPath="Item1"基本上是设置以下内容的简写ComboBox.ItemTemplate:

<DataTemplate>
    <ContentPresenter Content="{Binding Item1}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

现在WPF仅支持绑定到属性.这就是为什么它在你使用Tuples 时工作正常- 因为它的成员是属性,也是为什么它不适用于ValueTuples - 因为它的成员是字段.

在您的特定情况下,由于您仅将这些集合用作绑定源,因此您可以使用匿名类型来实现您的目标(其成员也是属性),例如:

private IEnumerable<object> GetTupleData()
{
    yield return new { Label = "displayItem1", Value = "valueItem1" };
    yield return new { Label = "displayItem2", Value = "valueItem2" };
    yield return new { Label = "displayItem3", Value = "valueItem3" };
}
Run Code Online (Sandbox Code Playgroud)

然后你可以设置你ComboBox的:

<ComboBox DisplayMemberPath="Label" SelectedValuePath="Value" (...) />
Run Code Online (Sandbox Code Playgroud)

  • 故事的道德,"ValueTuple"的项目作为字段而不是属性公开,WPF不支持绑定到字段. (4认同)