locked
WinRT ListView with itemsource as List<KeyValuePair<string, string>> error XAML (Windows 8.1 Universal App (XAML/C#))

    Question

  • I cant get this to work... (Windows 8 Universal App (XAML/C#) project) I have a listview that is bound to a List < KeyValuePair < string , string > >
    here is the code:

                        <ListView 
                           ItemsSource="{Binding Languages}" 
                           SelectedItem="{Binding SelectedLanguage, Mode=TwoWay}"
                           >
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Value}" FontSize="18" FontWeight="Light" />
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>

    and here is my C# code:

      private List<KeyValuePair<string, string>> _languages;
    public List<KeyValuePair<string, string>> Languages
    {
        get
        {
            if (_languages == null)
            {
                _languages = Utils.AvailableLanguages;
            }
    
            return _languages;
        }
    }
    
    public KeyValuePair<string, string> SelectedLanguage
    {
        get
        {
            return Languages.SingleOrDefault(x => x.Key == Utils.CurrentLanguage);
        }
        set
        {
            Utils.CurrentLanguage = value.Key;
            Api.Localization = value.Key;
            RaisePropertyChanged(() => CurrentLanguage);
        }
    }

    In my listview if I do a "Binding Value" I get nothing (no data is shown), however if i do Binding without the Value element it shows me both the Key and the Value of my List :

    <TextBlock Text="{Binding}" FontSize="18" FontWeight="Light" />

    it showns me this: http://i.imgur.com/F5ecYJI.png

    How do i make it so that i show only the Value of my list and not the key? thank you for your help!

    Edit: if i do this:

    <TextBlock Text="{Binding key}" FontSize="18" FontWeight="Light"  />

    or that

    <TextBlock Text="{Binding value}" FontSize="18" FontWeight="Light"  />

    same bug, :
    enter image description here

    Edit2: Utils.AvailableLanguages value:enter image description here



    Friday, November 14, 2014 6:13 AM

Answers

  • Hi DelaireD,

    Per my understanding, this problem is caused by the KeyValuePair object. The easiest way to work around is to use custom class. You can try the following code snippet.

    In XAML.

     <ListView x:Name="Listview1">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}" FontSize="18" FontWeight="Light" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
    

    In XAML.cs.

     private void Page_Loaded(object sender, RoutedEventArgs e)
            {
                Languages = new List<Language>();
                Languages.Add(new Language() { Key = "aa", Value = "1111" });
                Languages.Add(new Language() { Key = "bb", Value = "2222" });
                Languages.Add(new Language() { Key = "cc", Value = "3333" });
                Languages.Add(new Language() { Key = "dd", Value = "4444" });
                Listview1.ItemsSource = Languages;
            }
     public class Language
        {
            public string Key { get; set; }
            public string Value { get; set; }
            public override string ToString()
            {
                return Value;
            }
        }
    

    If you still have questions, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, November 17, 2014 3:02 AM
    Moderator