简体   繁体   中英

Xamarin.Forms Picker ItemDisplayBinding not working with static list

I was experimenting with picker using different item sources and when i tried using a static list, i keep getting a NullPointerException.

Xaml

<Picker ItemsSource="{x:Static Member=stat:Stat.ItemModList}"
                    ItemDisplayBinding="{Binding Name}" />

The picker is the only element in the xaml. I didn't change the code behind file. stat is the namespace for the static list file.

When only <Picker ItemsSource="{x:Static Member=stat:Stat.ItemModList}" /> is written the picker does work and the picker items are all displayed as ItemMod class' ToString()

When I add the ItemDisplayBinding="{Binding Name} it just does not work. I get the nullpointerexception when i click on the picker.

The other files:

Stat.cs - Static List

public static class Stat
    {
        public static List<ItemMod> ItemModList = new List<ItemMod>()
        {
            new ItemMod {Id = -1, Name = "Default"},
            new ItemMod {Id = 1, Name = "Item 1"},
            new ItemMod {Id = 2, Name = "Item 2"},
            new ItemMod {Id = 3, Name = "Item 3"},
            new ItemMod {Id = 4, Name = "Item 4"}
        };
    }

ItemMod.cs

public class ItemMod
    {
        public int Id;
        public string Name;
    }

The ItemDisplayBinding did not work as the BindingProperty Name was declared as a public field in the ItemMod class instead of a public property.
So in ItemMod.cs it should be public string Name {get;set;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM