Issue
Im trying to use a picker similar to html's
<select>
<option value="KEY">Value</option>
</select>
However I can't find anything relating to this. I have something along the lines of
List<KeyValuePair<int, string>> KVP = {1: "test", 2: "test2"}
Current xaml:
<StackLayout x:Name="PickerStack">
<Picker x:Name="DefaultPicker"
Title="---Select a map---"
ItemsSource="{Binding GPXList_PickerList}"
SelectedIndexChanged="PickerStack_Selected"
SelectedItem="{Binding PickerStack_Selection}" />
Current code behind:
var ResultConvert = JsonConvert.DeserializeObject<GPSAPI_GPXList[]>(ResultJSON);
List<int> gpxList_ID = ResultConvert.Select(x => x.gpxList_ID).ToList();
List<string> gpxList_TrailHead_Name = ResultConvert.Select(x => x.gpxList_TrailHead_Name).ToList();
GPXList_Dictionary = ResultConvert.ToDictionary(x => x.gpxList_ID, x => x.gpxList_TrailHead_Name);
//
// call for my picker to be populated
PickerView(GPXList_PickerItems);
}
//
// fill the picker with retrieved API values
public void PickerView(List<KeyValuePair<int, string>> PickerSource)
{
DefaultPicker.ItemsSource = PickerSource;
}
//
// observe the PickerStack and retrieve values on change
private void PickerStack_Selected(object sender, EventArgs e)
{
Picker PickerSelection = sender as Picker;
TrailText.Text = PickerSelection.SelectedItem.ToString();
}
And would like to just display to the user the Value but return to the program the Key
public List<KeyValuePair<int, string>> GPXList_PickerItems { get; set; }
Solution
As Jason commented, use ItemDisplayBinding
<Picker x:Name="DefaultPicker"
Title="---Select a map---"
ItemsSource="{Binding GPXList_PickerList}"
SelectedIndexChanged="PickerStack_Selected"
SelectedItem="{Binding PickerStack_Selection}"
ItemDisplayBinding="{Binding Value}" />
void PickerStack_Selected(object sender, EventArgs e)
{
if (sender is Picker picker)
int key = ((KeyValuePair<int, string>)picker.SelectedItem).Key;
}
Answered By - Cfun
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.