'UWP DataGridComboBoxColumn converter

I'm trying to create a UWP datagrid with dynamically created DataGridComboBoxColumn columns that display enum options, but with custom combobox labels. To do this I have:

List<EnumDisplay> enums = new List<EnumDisplay>();
EnumConverter converter = new EnumConverter(propertyInfo.PropertyType);
foreach (var kv in converter.Labels)
{
    enums.Add(new EnumDisplay { DisplayName = kv.Key, Value = kv.Value as Enum });
}
Binding binding = new Binding
{
    Path = new PropertyPath(*name of property relevant to this column*),
    Mode = BindingMode.TwoWay,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
    Converter = converter
};
DataGridComboBoxColumn col = new DataGridComboBoxColumn()
{                                                 
    ItemsSource = enums
    DisplayMemberPath = nameof(EnumDisplay.DisplayName),
    Binding = binding
};

To create the columns, using these classes:

internal class EnumDisplay
{
    public Enum Value { get; set; }
    public string DisplayName { get; set; }
}

public class EnumConverter : IValueConverter
{
    public Dictionary<string, object> Labels { get; } = new Dictionary<string, object>();
    internal EnumConverter(Type enumType)
    {
        // Make a dictionary of label names for each value in the enum
        foreach (object value in enumType.GetEnumValues())
        {
            // This static method converts the enum value to the desired string
            string label = Extensions.EnumValueToLabel(enumType, value);
            Labels.Add(label, value);
        }
    }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        // Lookup the key from the value
        return Labels.FirstOrDefault(x => x.Value.ToString() == value.ToString()).Key;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        // Lookup the value from the key
        return Labels[value.ToString()];
    }
}

However, this produces the error: *System.ArgumentException: 'The ItemsSource elements do not contain a property [name of property relevant to this column]. Ensure that the binding path has been set correctly.' *

I don't see why the name of the property used by the binding should be applied to the ItemSource of the comboboxes. What am I missing?

Thanks in advance.

uwp


Solution 1:[1]

*System.ArgumentException: 'The ItemsSource elements do not contain a property [name of property relevant to this column]. Ensure that the binding path has been set correctly.' *

I could reproduce your problem, it looks binding context error cause this problem, please feel free report this problem in community tool kit github. And currently we have a workaround that use string list to replace item list like official processing. And the other way is set binding EnumDisplay property same as DataGrid item one property.

For example, if your DataGrid item model contains a property called Description, you need set same property name for EnumDisplay. It will solve the above exception, but the default DataGridComboBoxColumn cell content will be null.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nico Zhu - MSFT