c# - WPF 为绑定(bind)的 ComboBox 更正编程绑定(bind)

标签 c# wpf visual-studio

我正在使用一个 wpf 应用程序,目前正在实现一个属性网格框,类似于您可能在 Visual Studios 的属性窗口中看到的那个。

当使用反射选择我的应用程序中的项目时,我会动态填充此属性窗口,但这意味着我也必须动态创建输入法。(文本框、复选框、组合框)取决于属性的类型。

由于这是动态生成的,我不得不以编程方式创建这些项目。

我有以下代码确定是从属性生成文本框还是组合框,然后绑定(bind)组合框。

      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)'

绑定(bind)到组合框的值来自枚举,但我不确定如何解决这个问题。

最佳答案

由于您要将 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);
}

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

关于c# - WPF 为绑定(bind)的 ComboBox 更正编程绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5608235/

相关文章:

c# - 国际奥委会设计资源

c# - "Manifest XML signature is not valid"

c# - 用户在 C# 中的图片框中放置文本

wpf - 在 Windows XP 中运行 .NET 4.0 WPF 应用程序时与 ICommand 相关的错误

windows - Visual Studio shell(独立 shell)有什么用?

c# - Java 铁杆专家的 Windows 开发

c# - C#中超出基本类型的数字

java - 在 Java 服务和 .NET 客户端之间传递数据的最佳方式是什么?

c# - WPF : MenuItem. 命令参数绑定(bind)设置为空

entity-framework - 如何在 Entity Framework 中添加外键关系?