user10200885
user10200885

Reputation:

Xamarin Forms - How to get selected item in picker?

I have a picker. I need to get the ProvinceID I tried to get the ProvinceID using the code below. but I cannot get the ProvinceID. I can only get the text inside the picker which is the province name. How can I get the ProvinceID and What is the difference between ItemsSource and ItemDisplayBinding?

codePicker.ItemsSource[codePicker.SelectedIndex].ToString() - I tried to use this code to see if I can get the ProvinceID.

Sample Data:
Province: Nueva Ecija - This is what the picker displays
ProvinceID: NE210 - This is what I need to get

<local:CustomPicker x:Name="provincePicker" SelectedIndexChanged="provincePicker_SelectedIndexChanged" Unfocused="provincePicker_Unfocused" SelectedItem="{Binding DisplayText}" ItemsSource="{Binding ProvinceID}" ItemDisplayBinding="{Binding DisplayText}" StyleClass="fieldForm" IsEnabled="True">
    <local:CustomPicker.FontFamily>
         <OnPlatform x:TypeArguments="x:String">
             <On Platform="Android" Value="HelveticaNeueLTPro-Lt.otf#HelveticaNeueLTPro-Lt"/>
         </OnPlatform>
    </local:CustomPicker.FontFamily>
</local:CustomPicker>

var getCode = conn.QueryAsync<RetailerGroupTable>("SELECT * FROM tblRetailerGroup WHERE RetailerCode=?", code);
var resultCount = getCode.Result.Count;

if (resultCount > 0)
{
   for (int i = 0; i < resultCount; i++)
   {
       var result = getCode.Result[i];
       provincePicker.SelectedItem = result.ProvinceID;
   }
}

Upvotes: 1

Views: 4628

Answers (1)

FreakyAli
FreakyAli

Reputation: 16439

What you need to do is quite simple actually:

  • Get Selected Item of the Picker something like this;

    var selectedItem = provincePicker.SelectedItem as RetailerGroupTable;
    var provinceId= selectedItem.ProvinceId;
    
  • I would also suggest you subscribe to the SelectedIndexChanged event in your picker so you know that there was a change in the picker selection this can be done in two ways :

    XAML

    <Picker SelectedIndexChanged="Picker_SelectedIndexChanged">
    

    C#

    provincePicker.SelectedIndexChanged+=Picker_SelectedIndexChanged;
    

What is the difference between ItemsSource and ItemDisplayBinding?

As per the Xamarin docs

ItemsSource of type IList, is the source list of items to display, which defaults to null. whereas When binding to a list of objects, the Picker must be told which property to display from each object. This is achieved by setting the ItemDisplayBinding property to the required property from each object.

Comments:

ItemsSource is an IList and hence has an Enumerator, how that works is it keeps track of how many items do you have in your list of items and thus creates that many spots to fill, ItemDisplayBinding, on the other hand, has nothing to do with any of these things all it does is it tells your Picker that I want this item to be displayed into the picker options when my Picker is in turn ready.

I hope I am making sense, Revert in case of queries.

UPDATE

XAML

<local:CustomPicker x:Name="provincePicker" SelectedIndexChanged="provincePicker_SelectedIndexChanged" Unfocused="provincePicker_Unfocused" ItemsSource="{Binding ProvinceID}" ItemDisplayBinding="{Binding DisplayText}" StyleClass="fieldForm" IsEnabled="True">
<local:CustomPicker.FontFamily>
     <OnPlatform x:TypeArguments="x:String">
         <On Platform="Android" Value="HelveticaNeueLTPro-Lt.otf#HelveticaNeueLTPro-Lt"/>
     </OnPlatform>
</local:CustomPicker.FontFamily>

C#

 Device.BeginInvokeOnMainThread(()=>{
 var result = conn.QueryAsync<RetailerGroupTable>("SELECT * FROM tblRetailerGroup WHERE RetailerCode=?", code).Result.FirstOrDefault();
provincePicker.SelectedItem = result;
                    });

Upvotes: 4

Related Questions