WPF DatagridComboBoxColumn displaymemberpath描述属性

时间:2016-07-17 09:10:39

标签: c# wpf datagrid datagridcolumn

DataGrid具有AutoGeneratedColumns = true。 某些属性是枚举,因此相应的列是DatagridComboBoxColumn。 枚举定义具有如下描述属性:

public enum MyEnum
{
    [Description("first")]
    FirstEnum,

    [Description("second")]
    SecondEnum
}

我已经有了一个实用工具方法来在ComboBox中显示Description属性:

public class EnumToItemsSource : MarkupExtension
{
    private readonly Type _type;

    public EnumToItemsSource(Type type)
    {
        _type = type;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Enum.GetValues(_type).Cast<object>().Select(e => new { Value = e,
            Description = GetEnumDescription((Enum)e) });
    }

    public static string GetEnumDescription(Enum enumObj)
    {
        FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
        object[] attributes = fieldInfo.GetCustomAttributes(false);
        if (attributes.Length == 0)
            return enumObj.ToString();
        else
        {
            DescriptionAttribute attrib = attributes.OfType<DescriptionAttribute>().FirstOrDefault();
            if (attrib != null)
                return attrib.Description;
            else
                return "No description";
        }
    }
}

我在xaml中使用它如下:

<ComboBox ItemsSource="{utils:EnumToItemsSource {x:Type myenums:MyEnum}}"
                              DisplayMemberPath="Description"
                              SelectedValuePath="Value"
                              SelectedValue="{Binding SomeProperty}"/>

现在我的问题是,如何将其应用于自动生成的列?

编辑首次尝试

我在AutoGeneratingColumn事件处理程序中尝试了第一个解决方案:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        DataGridComboBoxColumn col = e.Column as DataGridComboBoxColumn;
        if (col != null)
        {
            col.ItemsSource = EnumToIEnumerable.GetIEnumerable(e.PropertyType);
            col.DisplayMemberPath = "Value";
            col.SelectedValuePath = "Key";
            col.SelectedValueBinding = new Binding(e.PropertyName);
        }
    }

为此,我必须编写一个新的辅助静态方法来提供组合框项目源的列表:

public static class EnumToIEnumerable
{
    public static IEnumerable<KeyValuePair<Enum, string>> GetIEnumrable(Type type)
    {
        return Enum.GetValues(type).Cast<Enum>().Select((e) => new KeyValuePair<Enum, string>(e,
            EnumDescriptionConverter.GetEnumDescription((Enum)e)));
    }
}

它正在显示描述。但是程序无法将SelectedValue转换为绑定属性。它引发了无声的异常,组合框是红色的。

我尝试用英语翻译错误信息:

Conversion of EnumConverter is not possible from System.Collections.Generic.KeyValuePair
System.Windows.Data Error: 7 : ConvertBack cannot convert value '[Surface, m²]' (type 'KeyValuePair`2'). BindingExpression:Path=TypeQuantite; DataItem='MscaGridLineViewModel' (HashCode=39414053); target element is 'TextBlockComboBox' (Name=''); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: Conversion de EnumConverter impossible à partir de System.Collections.Generic.KeyValuePair`2[[System.Enum, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].

我不明白为什么它写的目标属性是'SelectedItem'因为SelectedValuePath包含一个Enum对象而且SelectedValueBinding将它绑定到一个枚举类型为“MyEnum”的属性。 / p>

1 个答案:

答案 0 :(得分:1)

答案:我只需添加第col.SelectedItemBinding = null;

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    DataGridComboBoxColumn col = e.Column as DataGridComboBoxColumn;
    if (col != null)
    {
        col.ItemsSource = EnumToIEnumerable.GetIEnumerable(e.PropertyType);
        col.DisplayMemberPath = "Value";
        col.SelectedValuePath = "Key";
        col.SelectedValueBinding = new Binding(e.PropertyName);
        col.SelectedItemBinding = null;
    }
}