1

我需要将枚举绑定到 DataGridTemplateColumn 内的组合框,但只有枚举具有的一些选项。
示例:
枚举选项: Unknow , One , Two , Three , Four , All
Bindable one : One , Two , Three , Four

有什么办法可以做到这一点?
非常感谢。

最好的祝福

4

3 回答 3

5

我有一个用于此的值转换器。它旨在绑定到可用于 ItemsSource 和 SelectedItem 的枚举类型的属性:

<ComboBox ItemsSource="{Binding Path=Day, Converter={StaticResource EnumToListConverter}, ConverterParameter='Monday;Friday'}" SelectedItem="{Binding Day}"/>

也可以通过直接引用枚举来使用:

<ComboBox ItemsSource="{Binding Source={x:Static sys:DayOfWeek.Sunday}, Converter={StaticResource EnumToListConverter}, ConverterParameter='Monday;Friday'}" Grid.Column="2"/>

这是转换器代码:

public class EnumToListConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is Enum))
            return null;

        string filters = parameter == null ? String.Empty : parameter.ToString();
        IEnumerable enumList;
        string[] splitFilters = filters != null ? filters.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) : new string[] { };
        List<string> removalList = new List<string>(splitFilters);
        Type enumType = value.GetType();
        Array allValues = Enum.GetValues(enumType);
        try
        {
            var filteredValues = from object enumVal in allValues
                                 where !removalList.Contains(Enum.GetName(enumType, enumVal))
                                 select enumVal;
            enumList = filteredValues;
        }
        catch (ArgumentNullException)
        {
            enumList = allValues;
        }
        catch (ArgumentException)
        {
            enumList = allValues;
        }
        return enumList;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2010-05-03T04:56:37.937 回答
0

复制要绑定到数组的枚举,然后绑定到数组。

于 2010-05-01T19:47:41.143 回答
0

也许这个纠结可以帮助你

将枚举属性数据绑定到 WPF 中的组合框

于 2010-05-02T02:39:01.370 回答