1
votes

I am trying to add a null default value to my Picker. But I get a null exception if one of the items in ItemsSource is null. Here is the Picker on my XAML: Picker

<Picker Grid.Row="0"
       x:Name="Control"
       IsVisible="False"
       SelectedIndexChanged="selectedIndexChanged" />

Here is the error and the values on ItemsSource:

Error

private void updateItemsSource(object oldValue, object newValue)
{
       if (oldValue is INotifyCollectionChanged oldObservable)
       {
           oldObservable.CollectionChanged -= onCollectionChanged;
       }

       Control.ItemsSource = ItemsSource;

       if (ItemsSource is INotifyCollectionChanged observable)
       {
           observable.CollectionChanged += onCollectionChanged;
       }

       setDefaultSelection();
}

Is there a way to make the picker accept null values? Maybe a renderer? Otherwise I will have to use an empty string as my default value. Which I don't think is the ideal for me.

1
Please paste the actual code, not images.haldo
Is your goal to just not show an initial value? If so, you may be able to set the SelectedIndex = -1.Andrew
See this answer for more details.Andrew
That is true. I was trying to add a new null field, instead of using -1 as the default selected index. It is dumb, I know. But that helped me =) Thank you @AndrewMarcelo Archiza Almeida
@MarceloArchizaAlmeida Hi , welcome to SO ! If using MVVM , this problem will be avoided . If using List<string> as ItemSource , you need to notice not adding null value manually .Junior Jiang

1 Answers

0
votes

If using MVVM with Picker in Xamarin Forms , you can avoid null value when set value for model .

For example , have a Model as follow :

public class Model 
{
    private string name;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (name != value & null != value)
            {
                name = value;
            }
            else
            {
                name = "Default Value";
            }
        }
    }
}

Used in ViewModel and initialized with a null value insdie it .

public class ViewModel
{

    public List<Model> monkeyList { set; get; }

    public ViewModel()
    {
        monkeyList = new List<Model>();
        monkeyList.Add(
            new Model() {
                Name = "Baboon"
            });

        monkeyList.Add(
           new Model()
           {
               Name = "Capuchin"
           });

        monkeyList.Add(
           new Model()
           {
               Name = "Squirrel"
           });
        monkeyList.Add(
           new Model()
           {
               Name = "Howler"
           });
        monkeyList.Add(
           new Model()
           {
               Name = null
           });
    }
}

Then in Xaml , Picker will show data no problem .

<Picker x:Name="picker"
        Title="Select a monkey"
        TitleColor="Red"
        ItemsSource="{Binding monkeyList}"
        ItemDisplayBinding="{Binding Name}">
</Picker>

The effect as follow :

enter image description here