2013-06-29 46 views

回答

3

綁定很容易。唯一的問題是,擴展方法GetNames()在Windows Phone中不可用。但是,你可以寫一個。

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>(); 
    } 
} 

一旦你有了它,很容易將它綁定到任何列表。

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());