0

ListView の DataTemplate でバインドが機能しないという問題があります。私のバインディング ターゲットは KeyValuePair です。(私は Windows 8 用の Metro アプリを使用しています)

私は辞書を持っています

Params = new Dictionary<string, string>();
Params.Add("Key1", "Value1");
Params.Add("Key1", "Value2");

私はそれをバインドしてみます:

<ListView ItemsSource="{Binding Params}">
    <ListView.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding Key}"></TextBlock>
           <TextBlock Text="{Binding Value}"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

しかし、KeyPairValue はこれに反応しません (何もバインドしません)。しかし、私がそのバインディングを行う場合:

<ListView ItemsSource="{Binding Params}">
    <ListView.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding}"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

そうですか: スクリーンショットの xaml バインディングを入力してください

初期のこのバインドは、Windows Phone 7 のアプリで正しく機能します。Windows 8 ではどうなりましたか?

4

2 に答える 2

3

指定してみてくださいPath=

<ListView ItemsSource="{Binding Path=Params}">
    <ListView.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding Path=Key}"></TextBlock>
           <TextBlock Text="{Binding Path=Value}"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

しかし、あなたはObservableDictionary

または、このバグに直面している可能性があります: http://social.msdn.microsoft.com/Forums/en-AU/winappswithcsharp/thread/234a17ad-975f-42f6-aa91-7212deda4190グーグルで見つけましたclrIkeyvaluepairimpl

于 2012-11-03T10:27:45.117 に答える
0

もう 1 つの解決策は、ディクショナリの代わりにカスタム キーと値のペアを持つリストを使用することです。その理由は、バインディング中にディクショナリに存在するキーと値のペアを一覧表示するために IEnumerable> が使用されるためです。System.Runtime.InteropServices.WindowsRuntime.CLRIKeyValuePairImpl に変換され、この型にバインドするときに問題があるため、実際には Dictionary ではなく KeyValuPair に問題があります。

したがって、次のようなクラスを作成します。

public class XamlFriendlyKeyValuePair<Tkey, TValue> 
{
    public TKey Key {get; set;}
    public TValue Value {get; set;} 
}

そして、このように使用するとうまくいくはずです:

Params = new List<XamlFriendlyKeyValuePair<string, string>>();
Params.Add{"Key1", "Value1"};
Params.Add{"Key1", "Value2"};

ソース: http://www.sohuaz.xyz/questions/683779/binding-a-dictionary-to-a-winrt-listbox

于 2016-02-12T20:55:03.700 に答える