Binding ComboBoxes to enums… in Silverlight!

后端 未结 4 652
醉梦人生
醉梦人生 2021-01-31 15:42

So, the web, and StackOverflow, have plenty of nice answers for how to bind a combobox to an enum property in WPF. But Silverlight is missing all of the features that make this

4条回答
  •  情歌与酒
    2021-01-31 16:05

    Here is the same setup for a Windows 8.1/Windows Phone universal App, main changes are:-

    • Missing DescriptionAttribute in the framework (or at least I can't find it)
    • Differences in how reflection works (using TypeInfo.Declared fields)

    It seems that the order of the XAML is important too, I had to put ItemsSource before SelectedIndex otherwise it didn't call the ItemsSource binding e.g.

    
    

    Code below

    namespace MyApp.Converters
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Reflection;
        using Windows.UI.Xaml.Data;
        public class EnumToIntConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                // Note: as pointed out by Martin in the comments on this answer, this line
                // depends on the enum values being sequentially ordered from 0 onward,
                // since combobox indices are done that way. A more general solution would
                // probably look up where in the GetValues array our value variable
                // appears, then return that index.
                return (int) value;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                return value;
            }
        }
    
        public class EnumToIEnumerableConverter : IValueConverter
        {
            private readonly Dictionary> _cache = new Dictionary>();
    
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                var type = value.GetType().GetTypeInfo();
                if (!_cache.ContainsKey(type))
                {
                    var fields = type.DeclaredFields.Where(field => field.IsLiteral);
                    var values = new List();
                    foreach (var field in fields)
                    {
                        var a = (DescriptionAttribute[]) field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (a != null && a.Length > 0)
                        {
                            values.Add(a[0].Description);
                        }
                        else
                        {
                            values.Add(field.GetValue(value));
                        }
                    }
                    _cache[type] = values;
                }
                return _cache[type];
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
        [AttributeUsage(AttributeTargets.Field)]
        public class DescriptionAttribute : Attribute
        {
            public string Description { get; private set; }
    
            public DescriptionAttribute(string description)
            {
                Description = description;
            }
        }
    }
    
        

    提交回复
    热议问题