5

Dictionary を WPF ListView および ListBox にバインドすることに関する多くの投稿を読みましたが、WinRT で動作する同等のコードを取得できません。

<Grid Margin="10" Width="1000" VerticalAlignment="Stretch">
        <ListBox Name="StatListView" ItemsSource="{Binding FooDictionary}" >
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <Grid Margin="6">
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="{Binding Key}" Margin="5" />
                            <TextBlock Text="{Binding Value}" Margin="5" />
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>


    public Dictionary<string, string> FooDictionary
    {
        get
        {
            Dictionary<string, string> temp = new Dictionary<string, string>();
            temp.Add("key1", "value1");
            temp.Add("key2", "value2");
            temp.Add("key3", "value3");
            temp.Add("key4", "value4");
            return temp;
        }
    }

適切なバインディングは何ですか?

4

2 に答える 2

8

出力ウィンドウのエラーは次のとおりです (最も有用な部分にトリミングされています)。

Error: Cannot get 'Key' value (type 'String') from type 
'System.Runtime.InteropServices.WindowsRuntime.CLRIKeyValuePairImpl`2
[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, ....

内部的に、WinRT は型を次のように変換しています。

System.Runtime.InteropServices.WindowsRuntime.CLRIKeyValuePairImpl<K, V>

DataTemplate に追加する場合:

<TextBlock Text="{Binding}" Margin="5" />

で上記の型を発行することがわかりますString, String

しかし、何らかの理由で、期待どおりに適切に処理されていません。インターネットでそのタイプを検索すると、Connect の問題に関する文書化されたバグがあることがわかります。

簡単な回避策は、KeyValuePair ではない単純なオブジェクトにデータを配置することです。

List<StringKeyValue> temp = new List<StringKeyValue>();
temp.Add(new StringKeyValue { Key = "key1", Value = "value1" } );
temp.Add(new StringKeyValue { Key = "key2", Value = "value2" });
temp.Add(new StringKeyValue { Key = "key3", Value = "value3" });
temp.Add(new StringKeyValue { Key = "key4", Value = "value4" });

this.DefaultViewModel["FooDictionary"] = temp;

public class StringKeyValue
{
    public string Key { get; set; }
    public string Value { get; set; }
}

余談ですが、少なくとも単純なテストでは、問題を引き起こしているのは Dictionary ではなく、上記KeyValuePairの型に変換されているのはオブジェクト インスタンスであるという事実です。リストを使用してインスタンスをCLRIKeyValuePairImplリストに追加しようとしましたが、それも失敗しました。KeyValuePair<string, string>

于 2012-11-11T16:23:36.580 に答える
0

独自のキーと値のペアを動的に生成することを含む回避策を考え出しました。

Dictionary を専門化している場合は、これを追加するだけです。

    public IEnumerable<MyPair<K, V>> Collection
    {
        get {
            foreach (var v in this)
            {
                MyPair<K, V> p = new MyPair<K, V>() { Key = v.Key, Value = v.Value };
                yield return p;
            }
         }
    }

ペア タイプを定義します。

public class MyPair<K, V>
{
    public K Key { get; set; }
    public V Value { get; set; }
}

また、毎回新しいオブジェクトを作成するように注意してください。一部のアイテムはオブジェクトを横切って戻り値を格納します。最初に行ったように MyPair を再利用しようとすると、すべてが最後のアイテムのように見える可能性があります。

于 2014-09-25T08:30:14.803 に答える