我正在使用wpf应用程序,目前正在实现属性网格框,类似于您在Visual Studio的属性窗口中可能看到的框 .
当我使用反射选择应用程序中的项目时,我会动态填充此属性窗口,但这意味着我必须动态创建输入方法 . (文本框,复选框,组合框),具体取决于属性的类型 .
由于这是动态生成的,因此我不得不以编程方式创建这些项目 .
我有以下代码,用于确定是从属性生成文本框还是组合框,然后绑定组合框 .
var attribute = (PropertyWindowAttribute) attributes[i];
if (attribute.Enumerated)
{
var combo = new ComboBox();
var binding = CreatePropertyBinding(Model.component, attribute.PropertyName);
combo.SetBinding(Selector.SelectedItemProperty, binding);
var enumValues = Enum.GetNames(Model.component.GetType().GetProperty(attribute.PropertyName).PropertyType);
foreach (var e in enumValues)
{
var item = new ComboBoxItem();
item.Content = e;
combo.Items.Add(item);
}
propertyList.Add(new PropertyElement(attribute.DisplayName, combo));
}
else
{
var binding = CreatePropertyBinding(Model.component, attribute.PropertyName);
var box = new TextBox();
box.SetBinding(TextBox.TextProperty, binding);
propertyList.Add(new PropertyElement(attribute.DisplayName, box));
}
}
我也有这个实用方法被引用
private static Binding CreatePropertyBinding(Component component, string path)
{
Binding binding = new Binding();
binding.Path = new PropertyPath(path);
binding.Mode = BindingMode.TwoWay;
binding.Source = component;
return binding;
}
我遇到的问题是当我转到我的应用程序,并尝试更改组合框的值时,我在我的应用程序中得到以下错误输出
System.Windows.Data Error: 23 : Cannot convert 'System.Windows.Controls.ComboBoxItem: Stretch' from type 'ComboBoxItem' to type 'System.Windows.HorizontalAlignment' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: EnumConverter cannot convert from System.Windows.Controls.ComboBoxItem.
at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
System.Windows.Data Error: 7 : ConvertBack cannot convert value 'System.Windows.Controls.ComboBoxItem: Stretch' (type 'ComboBoxItem'). BindingExpression:Path=ComponentHorizontalAlignment; DataItem='TextFieldComponent' (HashCode=31097589); target element is 'ComboBox' (Name=''); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: EnumConverter cannot convert from System.Windows.Controls.ComboBoxItem.
at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
at MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
绑定到组合框的值来自枚举,但我不确定这是什么修复 .
1 回答
由于您要将ComboBoxItems添加到ComboBox.Item集合,因此SelectedItem将是所选的ComboBoxItem . 因此,您有效地尝试将枚举属性设置为ComboBoxItem的实例 .
你的循环应该是这样的:
如果需要Style ComboBoxItems,可以使用ComboBox.ItemContainerStyle直接在它们上设置属性 . 或者您可以像现在一样添加ComboBoxItems,并在绑定到模型时使用SelectedValue和SelectedValuePath .