I have the following in my model:
public class Equipment
{
public enum Type
{
Detector,
VegetationClearance,
Removal,
Engaging
}
}
And in the view model:
private Equipment.Type _equipmentType;
public Equipment.Type EquipmentType
{
get { return _equipmentType; }
set
{
_equipmentType = value;
RaisePropertyChanged(() => EquipmentType);
}
}
And I want to use the values as an ItemsSource so that the user can select from the enumeration:
<Mvx.MvxSpinner
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource Equipment.Type; SelectedItem TypeSelection" />
This doesn't work at all. Is there a way to bind an enumeration as an ItemsSource?
EDIT: Better solution
As Anders commented,
Enum.GetValues()
is probably a better idea. One of the issues in binding to enums is that the identifier can't include spaces, so by default, binding will not give you a nice readable string.However, you can decorate your enum with a
Display
attribute. Reference System.ComponentModel.DataAnnotations.Now add the following properties to your ViewModel:
What we are going to do is create a Value Converter that converts an enum into a string for display that will return the Display Name attribute if present.
In order to use the value converter, you'll need to specify the item template and drop down template in your spinner:
And create the spinneritem/spinnerdropdownitem layouts:
Notice that we bind to
EnumDisplayName(.)
. That is the value converter and the.
means the current value which is the enum.I've added a sample on GitHub. https://github.com/kiliman/MvxSpinnerEnumSample