I use so enum in WP application:
public enum gender { man = 0, woman, other }
how can i write code that Listpicker items are gender enum items. To tell clear, i want user to select gender from Listpicker. Please, help.
I use so enum in WP application:
public enum gender { man = 0, woman, other }
how can i write code that Listpicker items are gender enum items. To tell clear, i want user to select gender from Listpicker. Please, help.
I assume you want to bind to a property of the enum type, like this?
public enum EnumType { Item1, Item2 }
public EnumType Property { get; set; }
This is how I did it:
(in the constructor)
theListPicker.ItemsSource = Enum.GetValues(typeof(EnumType));
(XAML)
<phone:PhoneApplicationPage
...
x:Name="_this"/>
...
<phone:PhoneApplicationPage.Resources>
<local:EnumIntConverter x:Name="enumIntConverter"/>
</phone:PhoneApplicationPage.Resources>
....
<toolkit:ListPicker ...
SelectedIndex="{Binding ElementName=_this, Path=Property, Converter={StaticResource enumIntConverter}, Mode=TwoWay}
(somewhere in your namespace)
public class EnumIntConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (int)(EnumType)value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Enum.GetValues(typeof(EnumType)).GetValue((int)value);
}
}
In my case, I also wanted to use the enum's Descriptions instead of their names, so I'm using this code instead of the "in the constructor" one-liner above:
Array rawValues = Enum.GetValues(typeof(EnumType));
List<string> values = new List<string>();
foreach (EnumType e in rawValues)
values.Add((typeof(EnumType).GetMember(e.ToString())[0].GetCustomAttributes(typeof(DescriptionAttribute), false)[0] as DescriptionAttribute).Description);
theListPicker.ItemsSource = values;