Jen143
Jen143

Reputation: 835

Picker function in Xamarin not showing items

Here is my code.

List<DriverStatusViewModel> driverStatus = PopulateDriverStatus();
            var selectedStatus = driverStatus.FirstOrDefault();

            var picker = new Picker { Title = "Select a status" };
            picker.SetBinding(Picker.ItemsSourceProperty, "driverStatus");
            picker.SetBinding(Picker.SelectedItemProperty, "selectedStatus");
            picker.ItemDisplayBinding = new Binding("Status");

Populate the List

private List<DriverStatusViewModel> PopulateDriverStatus()
        {
            var driverStatus = new List<DriverStatusViewModel>();
            var stat1 = new DriverStatusViewModel();
            stat1.EnumId = 1;
            stat1.Status = "At Lunch";
            driverStatus.Add(stat1);
            var stat2 = new DriverStatusViewModel();
            stat2.EnumId = 2;
            stat2.Status = "Break";
            driverStatus.Add(stat2);
            var stat3 = new DriverStatusViewModel();
            stat3.EnumId = 3;
            stat3.Status = "Delivering";
            driverStatus.Add(stat3);
            return driverStatus;
        }

Xaml codes for display

<Picker Title="Select a status"
                                ItemsSource="{Binding driverStatus}"
                                TextColor="Black"
                                BackgroundColor="Red"
                                ItemDisplayBinding="{Binding Name}"
                                SelectedItem="{Binding selectedStatus}" />

Below image is how it looks like when running in emulator enter image description here

Im very new to Xamarin, Please help me and Thank you in advance.

Upvotes: 0

Views: 2345

Answers (3)

Jen143
Jen143

Reputation: 835

This is what I did to solve the Picker.

//ViewModel

public class DriverStatusViewModel : INotifyPropertyChanged
    {
        List<DriverStatus> statusList;
        public List<DriverStatus> StatusList
        {
            get { return statusList; }
            set
            {
                if (statusList != value)
                {
                    statusList = value;
                    OnPropertyChanged();
                }
            }
        }
        public string Title { get; set; }
        DriverStatus selectedStatus;
        public DriverStatus SelectedStatus
        {
            get { return selectedStatus; }
            set
            {
                if (selectedStatus != value)
                {
                    selectedStatus = value;
                    OnPropertyChanged();
                }
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

//Model

public class DriverStatus
    {
        public int E_Id { get; set; }
        public string E_Name { get; set; }
    }

//cs file

BindingContext = new DriverStatusViewModel
            {
                StatusList = PopulateDriverStatus()
            };

//adding Static data for DriverStatus

private List<DriverStatus> PopulateDriverStatus()
        {
            var driverStatus = new List<DriverStatus>();
            var stat1 = new DriverStatus();
            stat1.E_Id = 1;
            stat1.E_Name = "At Lunch";
            driverStatus.Add(stat1);
            var stat2 = new DriverStatus();
            stat2.E_Id = 2;
            stat2.E_Name = "Break";
            driverStatus.Add(stat2);
            var stat3 = new DriverStatus();
            stat3.E_Id = 3;
            stat3.E_Name = "Delivering";
            driverStatus.Add(stat3);
            return driverStatus;
        }

//XAML file

<Picker x:Name="picker" 
     Title="Select new status"
     TextColor="Black"
     HorizontalOptions="Start"
     ItemsSource="{Binding StatusList}"
     ItemDisplayBinding="{Binding E_Name}"
     SelectedItem="{Binding SelectedStatus}"/>

<Label Text="{Binding SelectedStatus.E_Id}" IsVisible="False" x:Name="E_Id"/>

Special thanks to this answer Xamarin Forms Picker Binding I followed what he did.

Upvotes: 1

Robbit
Robbit

Reputation: 4358

In Xaml, add x:Name="pic":

<Picker Title="Select a monkey" 
        TextColor="Black"
        x:Name="pic"
        BackgroundColor="Red"
        ItemDisplayBinding="{Binding Name}"
        SelectedItem="{Binding selectedStatus}"/>

In your page add:

pic.ItemsSource = driverStatus;

Upvotes: 2

Martin Zikmund
Martin Zikmund

Reputation: 39082

The problem is that when you set a Binding in code, the string cannot "point" to a local variable. The binding is evaluated at runtime, so there would have to be a property with the name driverStatus and selectedDriver in the view model.

So instead of setting it as a local variable, create two properties DriverStatus and SelectedDriver and bind to them. Also you probably want to use INotifyPropertyChanged so that the UI is notified when the property values change.

Upvotes: 1

Related Questions