So, the question is simple. How do I apply styling for MvxSpinner.
There is plenty of answers regarding to styling a common Spinner, but most of them refer to this lines of code:
var adapter = new ArrayAdapter<string>(SupportActionBar.ThemedContext, Resource.Layout.event_selector, events);
adapter.SetDropDownViewResource(global::Android.Resource.Layout.SimpleSpinnerDropDownItem);
_eventSelector.Adapter = adapter;
However, I am not sure how to handle that in case of MvxSpinner. It probably uses some MvvmCross-specific own adapter. My binding is as follows:
<Mvx.MvxSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstCategoryGroups"
local:MvxBind="ItemsSource CategoryGroups; SelectedItem CategoryGroupdSelected"
/>
Any ideas? Both approaches: as styling the specific spinner element on the page as setting common style for any spinner. Thanks!
You can change the layout of the spinner (hence the style) directly in axml via two attributes:
local:MvxItemTemplate: Sets the layout of the selected item.local:MvxDropDownItemTemplate: Sets the layout of each item in the dropdownSo given a ViewModel with a collection
CategoryGroupsand theSelectedCategoryGroup, i.e.:and the
CategoryGroupclass:You can bind the collection to a spinner like this:
and in each template you define the layout/style of the item. Take into account that in the templates their
DataContextis an item (in this case aCategoryGroup). In my example, I want to display theNameof eachCategoryGroupthen I just need aTextViewand bind itsTextproperty to theNameof theCategoryGroup, i.e. the item'sDataContext:my_spinner_item.axml:
my_spinner_dropdown_item.axml:
Now if you want all of your spinner to have the same templates you can just set a style with those attributes or you can inherit
MvxSpinnerand set the templates programmatically.Apart from that if you want to use one of each template and not create one per each different binding you could have a base class
BaseItemViewModelwith anabstractproperty namedDescriptionand use that to bind in theView. So that in your VMs you always inherit fromBaseItemViewModeland overrideDescriptionproperty to have the value you want to display.Another way is to have a
WrappedItemViewModelwhere you pass the objectTyou want to be in the spinner and aFunc<T, string>that returns what the spinner displays (the description) and you have aDescriptionproperty that just invokes theFunc<T, string>. This way has the advantage that it doesn't make you inherit always from a VM and you can wrap almost any object in it adding the behaviour to be in a Spinner.