Windows phone 7 如何将枚举绑定到WP中的listpicker?
我在WP应用程序中使用so枚举: 公共枚举性别 { 人=0, 成年女子 另外 }Windows phone 7 如何将枚举绑定到WP中的listpicker?,windows-phone-7,Windows Phone 7,我在WP应用程序中使用so枚举: 公共枚举性别 { 人=0, 成年女子 另外 } 如何编写Listpicker项是性别枚举项的代码。为了说明清楚,我希望用户从Listpicker中选择性别。请帮忙。我想您想绑定到枚举类型的属性,像这样 public enum EnumType { Item1, Item2 } public EnumType Property { get; set; } 我就是这样做的: (在构造函数中) (XAML) 在我的例子中,我还想使用枚举的描述而不是它们的名称,因此我
如何编写Listpicker项是性别枚举项的代码。为了说明清楚,我希望用户从Listpicker中选择性别。请帮忙。我想您想绑定到枚举类型的属性,像这样
public enum EnumType { Item1, Item2 }
public EnumType Property { get; set; }
我就是这样做的:
(在构造函数中)
(XAML)
在我的例子中,我还想使用枚举的描述而不是它们的名称,因此我使用此代码而不是上面的“在构造函数中”一行:
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;
Array rawValues=Enum.GetValues(typeof(EnumType));
列表值=新列表();
foreach(原始值中的枚举类型e)
添加((typeof(EnumType).GetMember(e.ToString())[0].GetCustomAttributes(typeof(DescriptionAttribute),false)[0]作为DescriptionAttribute.Description);
ListPicker.ItemsSource=值;
您是否尝试过这样做?没有,但是现在我正在努力,谢谢这很有帮助,谢谢。但我现在还有其他问题。若Listpicker项计数为3(或小于3),pistpicker将在当前页面中作为组合框打开,但大于3,它将打开其他页面。不是组合框。我如何解决这个问题?同一页面中有3个以上的项目会溢出,并且在组合框中滚动不是一个选项。您可以尝试创建一个始终以全屏页面打开的listpicker。@Ceyhun Rehimov:将ItemCountThresholdValue设置为ListpickerEnum。GetValues在WP7中不可用
<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}
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);
}
}
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;