1

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.

haldo
  • 14,512
  • 5
  • 46
  • 52
Marcelo
  • 217
  • 1
  • 2
  • 11
  • Please paste the actual code, not images. – haldo Feb 04 '20 at 18:58
  • Is your goal to just not show an initial value? If so, you may be able to set the SelectedIndex = -1. – Andrew Feb 04 '20 at 20:30
  • See this [answer](https://stackoverflow.com/a/43826508/8395242) for more details. – Andrew Feb 04 '20 at 20:39
  • 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 @Andrew – Marcelo Feb 04 '20 at 23:49
  • @MarceloArchizaAlmeida Hi , welcome to SO ! If using MVVM , this problem will be avoided . If using `List` as `ItemSource` , you need to notice not adding null value manually . – Junior Jiang Feb 05 '20 at 08:01

1 Answers1

0

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

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • I use MVVM, this is a custom control with bindable properties, the binding to the view model is on a higher level. – Marcelo Feb 05 '20 at 22:11
  • But thank you anyways!! That is a useful tutorial, I was not the one that down voted you. – Marcelo Feb 05 '20 at 22:12
  • @MarceloArchizaAlmeida Okey , if using bindable properties , you can avoid null property be setted . If you have solved it , you can share solution then remember to mark it . – Junior Jiang Feb 06 '20 at 01:20