UWP DataGridComboBoxColumn 转换器
UWP DataGridComboBoxColumn converter
我正在尝试创建一个 UWP 数据网格,其中包含动态创建的 DataGridComboBoxColumn
列,这些列显示枚举选项,但带有自定义组合框标签。为此,我有:
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
};
要创建列,请使用这些 类:
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()];
}
}
但是,这会产生错误:
*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.'
*
我不明白为什么绑定使用的 属性 的名称应该应用于组合框的 ItemSource
。我错过了什么?
提前致谢。
*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.' *
我可以重现你的问题,它看起来是绑定上下文错误导致这个问题,请随时在社区工具包 github. And currently we have a workaround that use string list to replace item list like official processing 中报告这个问题。另一种方法是设置绑定 EnumDisplay
属性 与 DataGrid 项目一相同 属性.
例如,如果您的 DataGrid
物品模型包含一个名为 Description
的 属性,您需要为 EnumDisplay
设置相同的 属性 名称。它将解决上述异常,但默认的 DataGridComboBoxColumn 单元格内容将为空。
我正在尝试创建一个 UWP 数据网格,其中包含动态创建的 DataGridComboBoxColumn
列,这些列显示枚举选项,但带有自定义组合框标签。为此,我有:
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
};
要创建列,请使用这些 类:
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()];
}
}
但是,这会产生错误: *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.' *
我不明白为什么绑定使用的 属性 的名称应该应用于组合框的 ItemSource
。我错过了什么?
提前致谢。
*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.' *
我可以重现你的问题,它看起来是绑定上下文错误导致这个问题,请随时在社区工具包 github. And currently we have a workaround that use string list to replace item list like official processing 中报告这个问题。另一种方法是设置绑定 EnumDisplay
属性 与 DataGrid 项目一相同 属性.
例如,如果您的 DataGrid
物品模型包含一个名为 Description
的 属性,您需要为 EnumDisplay
设置相同的 属性 名称。它将解决上述异常,但默认的 DataGridComboBoxColumn 单元格内容将为空。