Forum Xamarin.iOS

How to set default value for picker

AmejackAmejack USMember ✭✭✭
Whenever picker opened its index values should be 0
Tagged:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited November 2019

    You could assign the value to a string property and bind it with Picker.SelectedItem in the model constructor method .

    Refer the code below :

    Xaml

       <Picker x:Name="picker" 
                FontAttributes="Bold" 
                FontSize="Large" 
                Margin="0,50,0,0"
                ItemsSource="{Binding ListData}"
                SelectedItem="{Binding SelectedLevel}">
        </Picker>
    

    Code

        [DesignTimeVisible(false)]
        public partial class MainPage : ContentPage
        {
    
            public Model model;
            public MainPage()
            {
                InitializeComponent();
    
                model = new Model();    
                this.BindingContext = model;
            }    
        }
    
        public class Model : INotifyPropertyChanged
        {
    
            public Model()
            {
                _listData = new List<string> { "A", "B", "C" };
                _selectedLevel = _listData[0];                  //Look at this Line !
            }
    
    
            private List<string> _listData;
            public List<string> ListData
            {
                get
                {
                    return _listData;
                }
                set
                {
                    _listData = value;
                    OnPropertyChanged(nameof(ListData));
                }
            }
    
    
    
            private string _selectedLevel;
            public string SelectedLevel
            {
                get
                {
                    return _selectedLevel;
                }
                set
                {
                    _selectedLevel = value;
                    OnPropertyChanged(nameof(SelectedLevel));
    
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    
  • AmejackAmejack USMember ✭✭✭
    This is for xamarini forms.. I need it in xamarini. Ios native
  • ColeXColeX Member, Xamarin Team Xamurai

    Place picker.Select(0, 0, true); in ViewDidLoad or ViewWillAppear method.

    public override void ViewDidLoad()
        {
            base.ViewDidLoad();
    
    
            var pickerModel = new PeopleModel(personLabel);
    
            picker.Model = pickerModel;
            picker.ShowSelectionIndicator = true;
    
            picker.Select(0, 0, true);  //this line
        }
    

    Refer https://stackoverflow.com/a/11777242/8187800 .

Sign In or Register to comment.