我有一个WPF应用程序的工作,并且在实现属性网格框,类似于一个你可能会在视觉工作室属性窗口中看到的进程是当前。
我取决于属性的类型动态地填充该属性窗口时,在我的应用项使用反射被选择,但是这意味着我必须创建输入方法动态以及(文本框,复选框,组合框)。
由于这是动态生成的,因此我不得不以编程方式创建这些项目。
我有以下的代码,确定它是否是生成一个文本框,或从属性组合框,然后将其结合在组合框。
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)'
绑定到组合框的值来自枚举,但我不确定解决方案是什么。
答案 0 :(得分:2)
由于您要将ComboBoxItems添加到ComboBox.Item集合中,因此SelectedItem将是所选的ComboBoxItem。因此,您有效地尝试将枚举属性设置为ComboBoxItem的实例。
你的循环应该是这样的:
var enumValues = Enum.GetValues(Model.component.GetType().GetProperty(attribute.PropertyName).PropertyType);
foreach (var e in enumValues) {
combo.Items.Add(e);
}
如果需要设置ComboBoxItem的样式,可以使用ComboBox.ItemContainerStyle直接在它们上设置属性。或者您可以像现在一样添加ComboBoxItems,并在绑定到模型时使用SelectedValue和SelectedValuePath。