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.
c# xamarin xamarin.forms
add a comment |
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.
c# xamarin xamarin.forms
add a comment |
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.
c# xamarin xamarin.forms
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
c# xamarin xamarin.forms
asked Nov 8 at 9:34
Prashant Pimpale
1,7332622
1,7332622
add a comment |
add a comment |
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}" />
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 theBindingContext
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
add a comment |
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}" />
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 theBindingContext
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
add a comment |
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}" />
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 theBindingContext
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
add a comment |
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}" />
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}" />
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 theBindingContext
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
add a comment |
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 theBindingContext
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password