0

「メトロ」アプリでデータバインドしようとしているリストボックスがあります。ここに私のxamlがあります:

    <ListBox x:Name="ImagesList" Margin="40" Grid.Row="1">
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Key}" />
            </StackPanel>
        </DataTemplate>
    </ListBox>

そして、ソースを作成しました:

        List<KeyValuePair<string, string>> items = 
            new List<KeyValuePair<string, string>>();

        items.Add(new KeyValuePair<string, string>("a", "a"));
        items.Add(new KeyValuePair<string, string>("b", "b"));
        items.Add(new KeyValuePair<string, string>("c", "c"));
        this.ImagesList.ItemsSource = items;

これにより、アプリa、b、cにテキストのリストが作成されると思います

ただし、代わりに、バインドした各要素に対して次のテキストを取得しています。

System.Runtime.InteropServices.CLRKeyBaluePairOmpl'2[System.String, System.String]

バインドしている型のフルネームが表示されているようです...何が間違っていますか?

4

2 に答える 2

1

に を割り当てる必要がありConverterますBinding

コンバーターを XAML リソースとして作成する

<src:KeyValueConverter:Key="KeyConverter"/>

バインディング コンバーターをテキスト ソースに追加する

Text="{Binding Path=ItemsList, Converter={StaticResource KeyConverter}}"

サンプル コンバーター コード

public class KeyValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var kvp = (KeyValuePair)value;
        return kvp.Key;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2012-11-15T21:59:05.123 に答える
0

私の Xaml も間違っていました。

 <ListBox x:Name="ImagesList" Margin="40" Grid.Row="1">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Value}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
于 2012-11-17T07:28:08.627 に答える