C# WPF绑定组合框的正确程序绑定

C# WPF绑定组合框的正确程序绑定,c#,wpf,visual-studio,C#,Wpf,Visual Studio,我正在使用一个wpf应用程序,目前正在实现一个属性网格框,类似于您在Visual Studio的“属性”窗口中看到的属性网格框 当使用反射选择应用程序中的项目时,我将动态填充此属性窗口,但这意味着我也必须根据属性的类型动态创建输入法。(文本框、复选框、组合框) 由于这是动态生成的,因此我不得不以编程方式创建这些项 我有以下代码来确定是从属性生成文本框还是组合框,然后绑定组合框 var attribute = (PropertyWindowAttribute) attributes[i

我正在使用一个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)' System.Windows.Data错误:23:无法使用默认转换将“en-US”区域性的“System.Windows.Controls.ComboxItem:Stretch”从类型“ComboxItem”转换为类型“System.Windows.HorizontalAlignment”;考虑使用转换器的绑定属性。NotSupportedException:'System.NotSupportedException:EnumConverter无法从System.Windows.Controls.ComboxItem转换。 位于System.ComponentModel.TypeConverter.GetConvertFromException(对象值) 位于System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext上下文、CultureInfo区域性、对象值) 位于System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext上下文、CultureInfo区域性、对象值) 在MS.Internal.Data.DefaultValueConverter.ConvertHelper(对象o,类型destinationType,DependencyObject targetElement,文化信息文化,布尔值isForward)' System.Windows.Data错误:7:ConvertBack无法转换值'System.Windows.Controls.ComboBoxItem:Stretch'(类型'ComboBoxItem')。BindingExpression:Path=ComponentHorizontalAlignment;DataItem='TextFieldComponent'(HashCode=31097589);目标元素是“组合框”(名称=“”);目标属性为“SelectedItem”(类型为“Object”)NotSupportedException:“System.NotSupportedException:EnumConverter无法从System.Windows.Controls.ComboxItem转换。 位于MS.Internal.Data.DefaultValueConverter.ConvertHelper(对象o,类型destinationType,DependencyObject targetElement,文化信息文化,布尔值isForward) 位于MS.Internal.Data.ObjectTargetConverter.ConvertBack(对象o、类型类型类型、对象参数、文化信息文化) 位于System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter转换器,对象值,类型sourceType,对象参数,CultureInfo区域性)'
绑定到组合框的值来自枚举,但我不确定修复方法是什么。

因为您正在将ComboBoxItems添加到ComboBox。Item collection,然后SelectedItem将是选中的ComboBoxItem。因此,您实际上是在尝试将枚举属性设置为ComboBoxItem的实例

您的循环应该是这样的:

var enumValues =  Enum.GetValues(Model.component.GetType().GetProperty(attribute.PropertyName).PropertyType);
foreach (var e in enumValues) {
    combo.Items.Add(e);
}

如果需要设置ComboBox项目的样式,可以使用ComboBox.ItemContainerStyle直接对其设置属性。或者您可以像现在一样添加ComboBoxItems,并在绑定到您的模型时使用SelectedValue和SelectedValuePath。

谢谢,简单回答,简单修复!