質問

Can someone please give me an example how to bind enum to listpicker on Windows Phone 8?

Can't find any on the internet... and the lack of documentation regarding this toolkit won't help.

Thanks

役に立ちましたか?

解決

Binding that is easy. The only problem is that the extension method GetNames() is not available in windows phone. However, you can write one.

public static class EnumExtensions {
  public static IEnumerable<string> GetNames<TEnum>() where TEnum : struct {
    var type = typeof(TEnum);
    if (!type.IsEnum)
      throw new ArgumentException(String.Format("Type '{0}' is not an enum", type.Name));

    return (
      from field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
      where field.IsLiteral
      select field.Name)
    .ToList<string>();
  }
}

Once you have that it is easy to bind it to any list.

public enum MyEnum { 
  v1, v2, v3
}

// Binding
myListPicker.ItemsSource = EnumExtensions.GetNames<MyEnum>();

// Getting selected value
var myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), myListPicker.SelectedItem.ToString());
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top