Bindable Picker in Xamarin Forms(MVVM)
Picker is a great control for selecting one option from the multiple options.
But If your developing your project using MVVM Framework then you surely get problem while using picker.
The reason is ITEMS property in picker is not bindable property .So Now you must be wondering that how to use a picker then in MVVM.To get this answer you might go from one forum to another forum and you get good custom bindable picker,but then all these picker have some problems.So I without any wasting your much more time suggest to use Jpc.BindablePicker
To Use Bindable Picker,first we need to add the namespace in the xaml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xmlns:bindablePicker="clr-namespace:JPC.BindablePicker;assembly=JPC.BindablePicker"> |
Now Add the BindablePicker in the xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
x:Class="NewFeature.MainPage" | |
xmlns:bindablePicker="clr-namespace:JPC.BindablePicker;assembly=JPC.BindablePicker"> | |
<StackLayout> | |
<bindablePicker:BindablePicker Title="Select" | |
ItemsSource="{Binding ItemsList}" | |
SelectedItem="{Binding ItemSelectedFromList}"/> | |
</StackLayout> | |
</ContentPage> |
As you have checked the “ItemSource” property is not
available in the regular picker.
Now Next Step is to bind the properties in the ViewModel.
Now Next Step is to bind the properties in the ViewModel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace NewFeature | |
{ | |
public class MainPageViewModel | |
{ | |
private object _itemSelectedFromList; | |
public MainPageViewModel() | |
{ | |
ItemsList = new List<string> | |
{ | |
"New York", | |
"Boston", | |
"Chicago", | |
}; | |
} | |
public List<string> ItemsList { get; set; } | |
public object ItemSelectedFromList | |
{ | |
get { return _itemSelectedFromList; } | |
set | |
{ | |
_itemSelectedFromList = value; | |
RaisePropertyChanged(() => ItemSelectedFromList); //in case you are using MVVM Light | |
} | |
} | |
} | |
} |
Now if you want to get the selectedItem from the picker
just use
ItemSelectedFromList.ToString();
So In this way you can use the bindable Picker.
If you like this tutorial or if you have any query to ask
then please comment below!
Nice blog. Thanks for sharing such great information.Develop xamarin application Hire Xamarin Mobile Developer , Xamarin Development services in Indore
ReplyDelete