Unable to set default value for Picker in Xamarin forms











up vote
0
down vote

favorite












I am using to Picker in my project. I am successfully able to display the All items in the Picker without an issue but for update functionality, I am not able to display the Default Item for Picker,



I have used SelectedItem and SelectedIndex but nothing worked in my case.



Can someone explain how that SelectedItem and SelectedIndex works,



My code:



<Picker Title="Select Gender" Margin="0,0,0,-5" ItemsSource="{Binding GenderList}"  x:Name="pkrGender" ItemDisplayBinding="{Binding Name}" >


In view Model:



public class MainViewModel : BaseViewModel 
{
// Which is bind to ItemSource of the picker
public ObservableCollection<Gender> GenderList { get; set; }

public class Gender: BaseViewModel {
private Genders _gender {
get;
set;
}

public Gender(Genders gender) {
this._gender = gender;
}


public string Name {
get {
return _gender.name;
}

set {
_gender.name = value;
OnPropertyChanged("Name");
}
}

}
public void APICALL()
{
// Getting values from API
}

}


And Genders Class:



public class Genders
{
public string Name {get;set;}
public string Code {get;set;}
}


And I am Adding the values in ViewModel like:



// From API Response Getting the list of Genders
var genderList = APICALL();
foreach (Genders gender in genderList)
{
GendersList.Add(new Genders(gender));
}


But not able to bind the default value In my case he selected the Male while registration so the Male should be the Default value for the picker.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am using to Picker in my project. I am successfully able to display the All items in the Picker without an issue but for update functionality, I am not able to display the Default Item for Picker,



    I have used SelectedItem and SelectedIndex but nothing worked in my case.



    Can someone explain how that SelectedItem and SelectedIndex works,



    My code:



    <Picker Title="Select Gender" Margin="0,0,0,-5" ItemsSource="{Binding GenderList}"  x:Name="pkrGender" ItemDisplayBinding="{Binding Name}" >


    In view Model:



    public class MainViewModel : BaseViewModel 
    {
    // Which is bind to ItemSource of the picker
    public ObservableCollection<Gender> GenderList { get; set; }

    public class Gender: BaseViewModel {
    private Genders _gender {
    get;
    set;
    }

    public Gender(Genders gender) {
    this._gender = gender;
    }


    public string Name {
    get {
    return _gender.name;
    }

    set {
    _gender.name = value;
    OnPropertyChanged("Name");
    }
    }

    }
    public void APICALL()
    {
    // Getting values from API
    }

    }


    And Genders Class:



    public class Genders
    {
    public string Name {get;set;}
    public string Code {get;set;}
    }


    And I am Adding the values in ViewModel like:



    // From API Response Getting the list of Genders
    var genderList = APICALL();
    foreach (Genders gender in genderList)
    {
    GendersList.Add(new Genders(gender));
    }


    But not able to bind the default value In my case he selected the Male while registration so the Male should be the Default value for the picker.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am using to Picker in my project. I am successfully able to display the All items in the Picker without an issue but for update functionality, I am not able to display the Default Item for Picker,



      I have used SelectedItem and SelectedIndex but nothing worked in my case.



      Can someone explain how that SelectedItem and SelectedIndex works,



      My code:



      <Picker Title="Select Gender" Margin="0,0,0,-5" ItemsSource="{Binding GenderList}"  x:Name="pkrGender" ItemDisplayBinding="{Binding Name}" >


      In view Model:



      public class MainViewModel : BaseViewModel 
      {
      // Which is bind to ItemSource of the picker
      public ObservableCollection<Gender> GenderList { get; set; }

      public class Gender: BaseViewModel {
      private Genders _gender {
      get;
      set;
      }

      public Gender(Genders gender) {
      this._gender = gender;
      }


      public string Name {
      get {
      return _gender.name;
      }

      set {
      _gender.name = value;
      OnPropertyChanged("Name");
      }
      }

      }
      public void APICALL()
      {
      // Getting values from API
      }

      }


      And Genders Class:



      public class Genders
      {
      public string Name {get;set;}
      public string Code {get;set;}
      }


      And I am Adding the values in ViewModel like:



      // From API Response Getting the list of Genders
      var genderList = APICALL();
      foreach (Genders gender in genderList)
      {
      GendersList.Add(new Genders(gender));
      }


      But not able to bind the default value In my case he selected the Male while registration so the Male should be the Default value for the picker.










      share|improve this question













      I am using to Picker in my project. I am successfully able to display the All items in the Picker without an issue but for update functionality, I am not able to display the Default Item for Picker,



      I have used SelectedItem and SelectedIndex but nothing worked in my case.



      Can someone explain how that SelectedItem and SelectedIndex works,



      My code:



      <Picker Title="Select Gender" Margin="0,0,0,-5" ItemsSource="{Binding GenderList}"  x:Name="pkrGender" ItemDisplayBinding="{Binding Name}" >


      In view Model:



      public class MainViewModel : BaseViewModel 
      {
      // Which is bind to ItemSource of the picker
      public ObservableCollection<Gender> GenderList { get; set; }

      public class Gender: BaseViewModel {
      private Genders _gender {
      get;
      set;
      }

      public Gender(Genders gender) {
      this._gender = gender;
      }


      public string Name {
      get {
      return _gender.name;
      }

      set {
      _gender.name = value;
      OnPropertyChanged("Name");
      }
      }

      }
      public void APICALL()
      {
      // Getting values from API
      }

      }


      And Genders Class:



      public class Genders
      {
      public string Name {get;set;}
      public string Code {get;set;}
      }


      And I am Adding the values in ViewModel like:



      // From API Response Getting the list of Genders
      var genderList = APICALL();
      foreach (Genders gender in genderList)
      {
      GendersList.Add(new Genders(gender));
      }


      But not able to bind the default value In my case he selected the Male while registration so the Male should be the Default value for the picker.







      c# xamarin xamarin.forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 9:34









      Prashant Pimpale

      1,7332622




      1,7332622
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Fundamentally, I think you're missing the OnPropertyChanged method in the GenderList property. As a result, your Picker is bound to an empty list.



          I found the following worked for me:



          private List<Genders> listOfGenders = new List<Genders>();

          public List<Genders> GenderList
          {
          get => listOfGenders;
          set
          {
          listOfGenders = value;
          OnPropertyChanged(nameof(GenderList));
          }
          }

          private void AddToGenderList(params Gender genders)
          {
          if (listOfGenders == null)
          {
          listOfGenders = new List<Genders>();
          }

          foreach(Gender gender in genders)
          {
          listOfGenders.Add(gender);
          }

          GendersList = listOfGenders;
          }


          How do the SelectedItem and SelectedIndex work?



          SelectedItem returns the selected object from the list associated to the Picker. In your case, it will return the Gender object that you selected.



          SelectedIndex returns the index of the item you selected in the list.



          Assigning a default value



          To assign a default value, first make sure that the list is not empty. For example, the code below (expanding from the code above) should work:



          (In your C#)



          private int currentSelectedIndex = 0;

          public int CurrentSelectedIndex
          {
          get => currentSelectedIndex;
          set
          {
          currentSelectedIndex = value;
          OnPropertyChanged(nameof(CurrentSelectedIndex));
          }
          }

          public MainViewModel()
          {
          AddToGenderList(new Gender
          {
          new Gender
          {
          Name = "Male",
          Code = "M"
          },
          new Gender
          {
          Name = "Female",
          Code = "F"
          },
          new Gender
          {
          Name = "Other",
          Code = "O"
          }
          // Add more if necessary
          });

          // Set the default to "Male"
          CurrentSelectedIndex = 0;
          }


          (In your XAML)



          <Picker 
          Title="Select Gender"
          Margin="0,0,0,-5"
          ItemsSource="{Binding GenderList}"
          SelectedIndex="{Binding CurrentSelectedIndex}"
          x:Name="pkrGender"
          ItemDisplayBinding="{Binding Name}" />





          share|improve this answer





















          • While using the list of string its working fine but the issue is with the Complex object. Let me try and get back to you!
            – Prashant Pimpale
            Nov 8 at 10:07










          • I have used the ItemSource property in .cs Should I remove that and use Binding instead?
            – Prashant Pimpale
            Nov 8 at 10:15










          • Only if you've set the BindingContext with your view model.
            – Tom
            Nov 8 at 10:17










          • Yes have used let me try with selected index, and thanks for the response!
            – Prashant Pimpale
            Nov 8 at 10:20













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204920%2funable-to-set-default-value-for-picker-in-xamarin-forms%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Fundamentally, I think you're missing the OnPropertyChanged method in the GenderList property. As a result, your Picker is bound to an empty list.



          I found the following worked for me:



          private List<Genders> listOfGenders = new List<Genders>();

          public List<Genders> GenderList
          {
          get => listOfGenders;
          set
          {
          listOfGenders = value;
          OnPropertyChanged(nameof(GenderList));
          }
          }

          private void AddToGenderList(params Gender genders)
          {
          if (listOfGenders == null)
          {
          listOfGenders = new List<Genders>();
          }

          foreach(Gender gender in genders)
          {
          listOfGenders.Add(gender);
          }

          GendersList = listOfGenders;
          }


          How do the SelectedItem and SelectedIndex work?



          SelectedItem returns the selected object from the list associated to the Picker. In your case, it will return the Gender object that you selected.



          SelectedIndex returns the index of the item you selected in the list.



          Assigning a default value



          To assign a default value, first make sure that the list is not empty. For example, the code below (expanding from the code above) should work:



          (In your C#)



          private int currentSelectedIndex = 0;

          public int CurrentSelectedIndex
          {
          get => currentSelectedIndex;
          set
          {
          currentSelectedIndex = value;
          OnPropertyChanged(nameof(CurrentSelectedIndex));
          }
          }

          public MainViewModel()
          {
          AddToGenderList(new Gender
          {
          new Gender
          {
          Name = "Male",
          Code = "M"
          },
          new Gender
          {
          Name = "Female",
          Code = "F"
          },
          new Gender
          {
          Name = "Other",
          Code = "O"
          }
          // Add more if necessary
          });

          // Set the default to "Male"
          CurrentSelectedIndex = 0;
          }


          (In your XAML)



          <Picker 
          Title="Select Gender"
          Margin="0,0,0,-5"
          ItemsSource="{Binding GenderList}"
          SelectedIndex="{Binding CurrentSelectedIndex}"
          x:Name="pkrGender"
          ItemDisplayBinding="{Binding Name}" />





          share|improve this answer





















          • While using the list of string its working fine but the issue is with the Complex object. Let me try and get back to you!
            – Prashant Pimpale
            Nov 8 at 10:07










          • I have used the ItemSource property in .cs Should I remove that and use Binding instead?
            – Prashant Pimpale
            Nov 8 at 10:15










          • Only if you've set the BindingContext with your view model.
            – Tom
            Nov 8 at 10:17










          • Yes have used let me try with selected index, and thanks for the response!
            – Prashant Pimpale
            Nov 8 at 10:20

















          up vote
          1
          down vote



          accepted










          Fundamentally, I think you're missing the OnPropertyChanged method in the GenderList property. As a result, your Picker is bound to an empty list.



          I found the following worked for me:



          private List<Genders> listOfGenders = new List<Genders>();

          public List<Genders> GenderList
          {
          get => listOfGenders;
          set
          {
          listOfGenders = value;
          OnPropertyChanged(nameof(GenderList));
          }
          }

          private void AddToGenderList(params Gender genders)
          {
          if (listOfGenders == null)
          {
          listOfGenders = new List<Genders>();
          }

          foreach(Gender gender in genders)
          {
          listOfGenders.Add(gender);
          }

          GendersList = listOfGenders;
          }


          How do the SelectedItem and SelectedIndex work?



          SelectedItem returns the selected object from the list associated to the Picker. In your case, it will return the Gender object that you selected.



          SelectedIndex returns the index of the item you selected in the list.



          Assigning a default value



          To assign a default value, first make sure that the list is not empty. For example, the code below (expanding from the code above) should work:



          (In your C#)



          private int currentSelectedIndex = 0;

          public int CurrentSelectedIndex
          {
          get => currentSelectedIndex;
          set
          {
          currentSelectedIndex = value;
          OnPropertyChanged(nameof(CurrentSelectedIndex));
          }
          }

          public MainViewModel()
          {
          AddToGenderList(new Gender
          {
          new Gender
          {
          Name = "Male",
          Code = "M"
          },
          new Gender
          {
          Name = "Female",
          Code = "F"
          },
          new Gender
          {
          Name = "Other",
          Code = "O"
          }
          // Add more if necessary
          });

          // Set the default to "Male"
          CurrentSelectedIndex = 0;
          }


          (In your XAML)



          <Picker 
          Title="Select Gender"
          Margin="0,0,0,-5"
          ItemsSource="{Binding GenderList}"
          SelectedIndex="{Binding CurrentSelectedIndex}"
          x:Name="pkrGender"
          ItemDisplayBinding="{Binding Name}" />





          share|improve this answer





















          • While using the list of string its working fine but the issue is with the Complex object. Let me try and get back to you!
            – Prashant Pimpale
            Nov 8 at 10:07










          • I have used the ItemSource property in .cs Should I remove that and use Binding instead?
            – Prashant Pimpale
            Nov 8 at 10:15










          • Only if you've set the BindingContext with your view model.
            – Tom
            Nov 8 at 10:17










          • Yes have used let me try with selected index, and thanks for the response!
            – Prashant Pimpale
            Nov 8 at 10:20















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Fundamentally, I think you're missing the OnPropertyChanged method in the GenderList property. As a result, your Picker is bound to an empty list.



          I found the following worked for me:



          private List<Genders> listOfGenders = new List<Genders>();

          public List<Genders> GenderList
          {
          get => listOfGenders;
          set
          {
          listOfGenders = value;
          OnPropertyChanged(nameof(GenderList));
          }
          }

          private void AddToGenderList(params Gender genders)
          {
          if (listOfGenders == null)
          {
          listOfGenders = new List<Genders>();
          }

          foreach(Gender gender in genders)
          {
          listOfGenders.Add(gender);
          }

          GendersList = listOfGenders;
          }


          How do the SelectedItem and SelectedIndex work?



          SelectedItem returns the selected object from the list associated to the Picker. In your case, it will return the Gender object that you selected.



          SelectedIndex returns the index of the item you selected in the list.



          Assigning a default value



          To assign a default value, first make sure that the list is not empty. For example, the code below (expanding from the code above) should work:



          (In your C#)



          private int currentSelectedIndex = 0;

          public int CurrentSelectedIndex
          {
          get => currentSelectedIndex;
          set
          {
          currentSelectedIndex = value;
          OnPropertyChanged(nameof(CurrentSelectedIndex));
          }
          }

          public MainViewModel()
          {
          AddToGenderList(new Gender
          {
          new Gender
          {
          Name = "Male",
          Code = "M"
          },
          new Gender
          {
          Name = "Female",
          Code = "F"
          },
          new Gender
          {
          Name = "Other",
          Code = "O"
          }
          // Add more if necessary
          });

          // Set the default to "Male"
          CurrentSelectedIndex = 0;
          }


          (In your XAML)



          <Picker 
          Title="Select Gender"
          Margin="0,0,0,-5"
          ItemsSource="{Binding GenderList}"
          SelectedIndex="{Binding CurrentSelectedIndex}"
          x:Name="pkrGender"
          ItemDisplayBinding="{Binding Name}" />





          share|improve this answer












          Fundamentally, I think you're missing the OnPropertyChanged method in the GenderList property. As a result, your Picker is bound to an empty list.



          I found the following worked for me:



          private List<Genders> listOfGenders = new List<Genders>();

          public List<Genders> GenderList
          {
          get => listOfGenders;
          set
          {
          listOfGenders = value;
          OnPropertyChanged(nameof(GenderList));
          }
          }

          private void AddToGenderList(params Gender genders)
          {
          if (listOfGenders == null)
          {
          listOfGenders = new List<Genders>();
          }

          foreach(Gender gender in genders)
          {
          listOfGenders.Add(gender);
          }

          GendersList = listOfGenders;
          }


          How do the SelectedItem and SelectedIndex work?



          SelectedItem returns the selected object from the list associated to the Picker. In your case, it will return the Gender object that you selected.



          SelectedIndex returns the index of the item you selected in the list.



          Assigning a default value



          To assign a default value, first make sure that the list is not empty. For example, the code below (expanding from the code above) should work:



          (In your C#)



          private int currentSelectedIndex = 0;

          public int CurrentSelectedIndex
          {
          get => currentSelectedIndex;
          set
          {
          currentSelectedIndex = value;
          OnPropertyChanged(nameof(CurrentSelectedIndex));
          }
          }

          public MainViewModel()
          {
          AddToGenderList(new Gender
          {
          new Gender
          {
          Name = "Male",
          Code = "M"
          },
          new Gender
          {
          Name = "Female",
          Code = "F"
          },
          new Gender
          {
          Name = "Other",
          Code = "O"
          }
          // Add more if necessary
          });

          // Set the default to "Male"
          CurrentSelectedIndex = 0;
          }


          (In your XAML)



          <Picker 
          Title="Select Gender"
          Margin="0,0,0,-5"
          ItemsSource="{Binding GenderList}"
          SelectedIndex="{Binding CurrentSelectedIndex}"
          x:Name="pkrGender"
          ItemDisplayBinding="{Binding Name}" />






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 10:02









          Tom

          533311




          533311












          • While using the list of string its working fine but the issue is with the Complex object. Let me try and get back to you!
            – Prashant Pimpale
            Nov 8 at 10:07










          • I have used the ItemSource property in .cs Should I remove that and use Binding instead?
            – Prashant Pimpale
            Nov 8 at 10:15










          • Only if you've set the BindingContext with your view model.
            – Tom
            Nov 8 at 10:17










          • Yes have used let me try with selected index, and thanks for the response!
            – Prashant Pimpale
            Nov 8 at 10:20




















          • While using the list of string its working fine but the issue is with the Complex object. Let me try and get back to you!
            – Prashant Pimpale
            Nov 8 at 10:07










          • I have used the ItemSource property in .cs Should I remove that and use Binding instead?
            – Prashant Pimpale
            Nov 8 at 10:15










          • Only if you've set the BindingContext with your view model.
            – Tom
            Nov 8 at 10:17










          • Yes have used let me try with selected index, and thanks for the response!
            – Prashant Pimpale
            Nov 8 at 10:20


















          While using the list of string its working fine but the issue is with the Complex object. Let me try and get back to you!
          – Prashant Pimpale
          Nov 8 at 10:07




          While using the list of string its working fine but the issue is with the Complex object. Let me try and get back to you!
          – Prashant Pimpale
          Nov 8 at 10:07












          I have used the ItemSource property in .cs Should I remove that and use Binding instead?
          – Prashant Pimpale
          Nov 8 at 10:15




          I have used the ItemSource property in .cs Should I remove that and use Binding instead?
          – Prashant Pimpale
          Nov 8 at 10:15












          Only if you've set the BindingContext with your view model.
          – Tom
          Nov 8 at 10:17




          Only if you've set the BindingContext with your view model.
          – Tom
          Nov 8 at 10:17












          Yes have used let me try with selected index, and thanks for the response!
          – Prashant Pimpale
          Nov 8 at 10:20






          Yes have used let me try with selected index, and thanks for the response!
          – Prashant Pimpale
          Nov 8 at 10:20




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204920%2funable-to-set-default-value-for-picker-in-xamarin-forms%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)