2012-11-03 37 views
0

我有一個問題在ListView的DataTemplate中獲取綁定工作。我的綁定目標是一個KeyValuePair。 (我與Metro應用可在Windows 8)字典<字符串,字符串>不綁定在xaml(Metro App)

我有一個字典

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> 

我看到: enter screenshot xaml binding

月初綁定正確的應用程序爲Windows Phone 7的Windows 8中發生了什麼工作?

回答

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

或者你可能只是對抗這個bug:http://social.msdn.microsoft.com/Forums/en-AU/winappswithcsharp/thread/234a17ad-975f-42f6-aa91-7212deda4190我發現通過谷歌搜索clrIkeyvaluepairimpl

+0

我嘗試使用ObservableDictionary或指定路徑,但它沒有幫助。 Perhars這是一個錯誤。 :(你知道如何解決這段時間? – iJoy

+0

沒有對不起,請按照我找到的bug的鏈接。 – weston

+0

哦,他們關閉了bug ...但沒有修復 – iJoy

0

另一個解決方案是使用具有自定義鍵/值對的列表而不是字典。原因是IEnumerable>將用於列出綁定期間字典中存在的鍵/值對。問題出在KeyValuPair上,實際上不是Dictionary,因爲它被轉換爲System.Runtime.InteropServices.WindowsRuntime.CLRIKeyValuePairImpl,並且綁定到這種類型時會出現問題。

因此,創建一個類,如:

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

相關問題