私はコンボボックスを持っており、そのItemsSourceをIEnumerable<(string,string)>
にバインドしたいと思います。 DisplayMemberPathを設定しないと、それは機能し、項目にToString()
を呼び出した結果がドロップダウンエリアに表示されます。それにもかかわらず、私がDisplayMemberPath="Item1"
と設定しても、もう何も表示されません。私は古典的なTuple
タイプを使用すると、期待どおりに動作することがわかる次のサンプルを作成しました。ValueTupleはDisplayMemberPathをサポートしていません。コンボボックス、WPF
デバッグ時に、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();
}
}
}
このサンプルでは、最初のコンボボックスにデータを適切に表示されますが、2番目には何も表示されません。
どうしてですか?
物語のモラル、 'ValueTuple'のアイテムはフィールドではなくプロパティとして公開され、WPFはありませんされていますフィールドへのバインドをサポートしていません。 –