Reputation: 745
Here is my code in xaml
<Picker Title="Privacy" SelectedItem="{Binding PrivacyLevel}">
<Picker.Items>
<x:String>Public</x:String>
<x:String>Private</x:String>
</Picker.Items>
</Picker>
The property PrivacyLevel is of integer type. I want to store 1 if public and 2 if Private. How?
Upvotes: 2
Views: 1503
Reputation: 2666
You can use a class to represent your items, maybe a PrivacyLevel
class. Instead of binding your Picker against a string's list you can use a List<PrivacyLevel>
. PrivacyLevel class contains both, the text you want to display and the value you want to persist/store/use.
public class PrivacyLevel
{
public string Name { get; set; }
public int Value { get; set; }
}
Then populate your ViewModel with the options.
public List<PrivacyLevel> Privacies { get; set; } = new List<PrivacyLevel>()
{
new PrivacyLevel(){Name = "Public",Value = 1},
new PrivacyLevel(){Name = "Private",Value = 2}
};
private PrivacyLevel _privacy;
public PrivacyLevel Privacy{
get{
return _privacy;
}
set{
_privacy=value;
OnPropertyChanged();
}
}
And finally,
<Picker Title="Privacy"
ItemsSource="{Binding Privacies}"
SelectedItem="{Binding Privacy}"
ItemDisplayBinding="{Binding Name}"/>
Remeber that now you have a SelectedItem which is an object and if you want to use the Value, you need access to its property.
Upvotes: 3