Search code examples
.netxamlbindingwindows-8windows-runtime

Dictionary<String, String> not binding in xaml (Metro App)


I've got a problem get binding working in an DataTemplate of a ListView. My binding target is a KeyValuePair. (I work with Metro App for Windows 8)

I have an Dictionary

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

I try bind it:

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

But KeyPairValue dont react on this (nothing bind). But if i do that binding:

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

I see: enter screenshot xaml binding

Early this binding work correctly in app for Windows Phone 7. What happened in Windows 8?


Solution

  • Try specifying 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>
    

    But you might need an ObservableDictionary

    Or you may just be up against this bug: http://social.msdn.microsoft.com/Forums/en-AU/winappswithcsharp/thread/234a17ad-975f-42f6-aa91-7212deda4190 which I found by googling clrIkeyvaluepairimpl