1
votes

Evening All,

Learning Xamarin forms..attempting to add a picker with numeric values...(using https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/picker/populating-itemssource)

I have used the example on this page to get the picker populated from the view...which works fine...however I want to populate the picker from code behind...

<---XAML--->
      <Picker Grid.Column="4"  Grid.Row="2" ItemsSource="{Binding pickerSource}"/>

<---c#---->
         var pickerList = new List<string>();
                pickerList.Add("1");
                pickerList.Add("2");
                pickerList.Add("3");
                pickerList.Add("4");
                pickerList.Add("5");
                pickerList.Add("6");
                pickerList.Add("7");
                pickerList.Add("8");
                pickerList.Add("9");
                pickerList.Add("10");

                var pickerSource = new Picker { Title = "Quantity", TitleColor = Color.Red };
                pickerSource.ItemsSource = pickerList;

Picker is appearing on app but when selected, its not populated with any values...why isnt this binding properly anyone?

Thank you

Also...as a side note if anyone is aware of a tool that contains all numeric values instead of me manually having to populate it with 1,2,3 etc..

Thanks Again


Thanks to @Jason for the reply...From here I have went with the following:

---xaml--

<Picker Grid.Column="4"  Grid.Row="2" ItemsSource="{Binding pickerSource}"/>

---c#----

public List<string> pickerSource { get; set; }

 public void PopulateQuantityPicker()
        {
            var pickerList = new List<string>();
            pickerList.Add("1");
            pickerList.Add("2");
            pickerList.Add("3");
            pickerList.Add("4");
            pickerList.Add("5");
            pickerList.Add("6");
            pickerList.Add("7");
            pickerList.Add("8");
            pickerList.Add("9");
            pickerList.Add("10");

            pickerSource = pickerList;

            this.BindingContext = this;
}

The picker is on the app, but it is not populated, it is empty. When I Click on it I get the following:

(also the code is hitting the PopulateQuantityPicker())

enter image description here

1

1 Answers

0
votes

here you are binding your ItemsSource to pickerSource

<Picker Grid.Column="4"  Grid.Row="2" ItemsSource="{Binding pickerSource}"/>

in your code behind, you need a public property named pickerSource. You can only bind to public properties

public List<string> pickerSource { get; set }

// assign the data to your ItemsSource
pickerSource = pickerList;

// also be sure to set the BindingContext
BindingContext = this;

// this is creating a new picker named pickerSource.  You have already done 
// this in your XAML.  This is NOT NEEDED
var pickerSource = new Picker { Title = "Quantity", TitleColor = Color.Red };
pickerSource.ItemsSource = pickerList;

if you want to do this from the code behind WITHOUT using binding, you first need to assign an x:name to your control

<Picker x:Name="myPicker" Grid.Column="4"  Grid.Row="2" />

then in the code behind

myPicker.ItemsSource = pickerList;