Ever wanted to have an Icon element attached to a Xamarin.Forms.Picker control? Then you’re at the right place. Welcome to another lightening short post of me hacking around Xamarin.Forms elements!
That’s what we gonna be build yol!
Usually you would think you need to implement a Custom Renderer to get this done or use a third party control! I say NO! NO! NO!
You can easily do this right from Xamarin.Forms without any native coding or 3rd party library, let me explain.
In a Xamarin.Forms Grid layout we could place Elements on top of each other, using this simple advantage, we’re going to place an Image as an icon under a Picker control, and of course we’ll be setting the Background color of the Picker to Transparent! 😉 Simple right?!
Behold the golden XAML code!
There you have it just like we discussed in the recipe, we have placed our Picker control on top of the Image control, and we’re using a Grid to bring all of this together. If you look closely, we are using two columns, the Picker is spread across both columns, and the Icon Image is only added to the last column, with a fixed width of 25 units, thereby aligning the Icon to the right most corner of the Picker from underneath it. 😀
You can set the WidthRequest to whatever the value you prefer. And as of Platform specific values we’re setting the Grid Height accordingly to the best appearance of Android and iOS separately, you’re in full liberty to change them as you wish.
Important: You could also move that whole piece of XAML to a separate XAML file, so that you could set it up as a reusable Control in your project! 😉
Pretty straight forward eh!
Fire it up!
There you have it running on Android and iOS!
Grab it on Github!
Well then, that’s it for now. More awesome stuff on the way!
Cheers! 😀 share the love!