我有一个下拉框,想要将其ItemsSource绑定到一个IEnumerable<(string, string)>。如果我不设置DisplayMemberPath,则它可以工作,并在下拉区域中显示调用项的ToString()的结果。然而,当我设置DisplayMemberPath="Item1"时,它就不再显示任何内容了。我制作了以下示例,您可以看到如果我使用经典的Tuple类型,它会按预期工作。
调试时,我已检查valuetuple也具有Item1和Item2作为属性。
我的XAML:
调试时,我已检查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>
我的代码后端:
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();
}
}
}
运行时,这个示例将在第一个组合框中正确显示数据,但在第二个组合框中不会显示任何内容。
为什么会发生这种情况?
ValueTuple
的项被公开为字段而不是属性,并且 WPF 不支持绑定到字段。 - Jeff Mercado