C# WPF组合框:无法来回转换所选项目(除此之外,在所有情况下都有效)

C# WPF组合框:无法来回转换所选项目(除此之外,在所有情况下都有效),c#,.net,wpf,mvvm,combobox,C#,.net,Wpf,Mvvm,Combobox,背景 我创建了一个简单的类,用于填充分配给组合框上的ItemSource属性的集合。当源是需要良好描述符文本的枚举列表时,我会使用此选项 public class BindableEnumerationItem<T> : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate { }; private T _item; pub

背景

我创建了一个简单的类,用于填充分配给组合框上的
ItemSource
属性的集合。当源是需要良好描述符文本的枚举列表时,我会使用此选项

public class BindableEnumerationItem<T> : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private T _item;

    public BindableEnumerationItem(T item, string displayText)
    {
        _item = item;
        _displayText = displayText;
    }

    private string _displayText;
    public string DisplayText
    {
        get { return _displayText; }
        set
        {
            if (value != _displayText)
            {
                _displayText = value;
                PropertyChanged(this, new PropertyChangedEventArgs("DisplayText"));
            }
        }
    }

    public T Item
    {
        get { return _item; }
        set
        {
            _item = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Item"));
        }
    }
}
接下来,在我的xaml中创建一个组合框。假设上面的初始化片段来自一个视图模型,该视图模型是我的xaml对象上的
DataContext
对象

<ComboBox x:Name="ComboboxAccountType" 
    ItemsSource="{Binding DisplayAccountTypes}"
    SelectedItem="{Binding SelectedDisplayAccountType}"
    DisplayMemberPath="DisplayText"
    SelectedValuePath="Item" >

仅供参考,
SelectedDisplayAccountType
是我的虚拟机上的一个枚举属性

问题

现在,我已经多次使用此设置。这很有效。我可以切换组合框的值,它会在后端更新。然而,在我最近的实现中,当我尝试使用当前组合框时,我遇到了以下错误

System.Windows.Data错误:23:无法转换
MyApp.ViewModel.Helpers.BindableEnumerationItem`1[MyApp.ViewModel.DisplayAccountType]
从类型
BindableEnumerationItem`1
到类型 默认为“en-US”区域性的MyApp.ViewModel.DisplayAccountType 转换;考虑使用转换器的绑定属性。 NotSupportedException:'System.NotSupportedException:EnumConverter 无法从转换
MyApp.ViewModel.Helpers.BindableEnumerationItem`1[[MyApp.ViewModel.DisplayAccountType
, MyApp.ViewModel,版本=1.0.0.0,区域性=中性, PublicKeyToken=null]]。在 System.ComponentModel.TypeConverter.GetConvertFromException(对象 价值)在 System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext 上下文、文化信息文化、对象值)位于 System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext 上下文、文化信息文化、对象值)位于 MS.Internal.Data.DefaultValueConverter.ConvertHelper(对象o,类型 destinationType,DependencyObject targetElement,CultureInfo区域性, 布尔值(向前)'

System.Windows.Data错误:7:ConvertBack无法转换值
MyApp.ViewModel.Helpers.BindableEnumerationItem`1[MyApp.ViewModel.DisplayAccountType]
(键入BindableEnumerationItem`1)。 BindingExpression:Path=SelectedDisplayAccountType; DataItem='CredentialsManagementViewModel'(HashCode=58609412);目标 元素是“ComboBox”(Name='ComboboxAccountType');目标属性为 'SelectedItem'(键入'Object') NotSupportedException:'System.NotSupportedException:EnumConverter 无法从转换 MyApp.ViewModel.Helpers.BindableEnumerationItem`1[[MyApp.ViewModel.DisplayAccountType, MyApp.ViewModel,版本=1.0.0.0,区域性=中性, PublicKeyToken=null]]。在 MS.Internal.Data.DefaultValueConverter.ConvertHelper(对象o,类型 destinationType,DependencyObject targetElement,CultureInfo区域性, 布尔值(向前)在 MS.Internal.Data.ObjectTargetConverter.ConvertBack(对象o,类型 类型、对象参数、CultureInfo区域性)位于 System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter 转换器、对象值、类型sourceType、对象参数、, 文化信息(文化)'

这件事困扰了我好几个小时。我没有做什么特别的事。事实上,上面的代码就是坏掉的生产代码。我可以复制并粘贴另一个使用此设置的示例,但是,除了每个属性/选择器各自上下文的名称/值不同之外,它们几乎完全相同

我明白这些错误是在说什么,但就我所做的而言,它们没有意义。我没有尝试从
BindableEnumerationItem
转换为我的枚举。我应该绑定到我的
BindableEnumerationItem
上的
Item
属性,它当然应该是我的枚举


也许我只需要一双眼睛,但我看不出问题所在。谢谢。

问题是您的项目类型为
BindableEnumerationItem
,但您将
SelectedItem
绑定到类型为
DisplayAccountType
的属性

有四种解决方案:

  • 改用
    SelectedIndex
  • 改用
    SelectedValue
  • 使
    DisplayAccountType
    成为
    DisplayAccountType
    项的集合
  • SelectedDisplayAccountType
    设置为
    BindableEnumerationItem
    类型

  • 我想问题在于您的
    displaycounttype
    不是
    BindableEnumerationItem
    的一部分,因为您使用的是泛型类型。如果您使用的不是
    public T Item
    ,而是
    public DisplayAccountType Item
    ,我认为它应该可以工作。您可以尝试放弃它。我认为通用方法根本没有问题。我用同样的。您是否尝试过使用
    SelectedValue
    来代替o
    SelectedItem
    ?@Mat…当您看到它时,它是如此明显。这就是为什么我需要第二双眼睛。在所有其他位置,我使用的是
    SelectedValue
    ,而不是
    SelectedItem
    。更改引用已修复我的应用程序。回答以下问题以获得答案。再次感谢。@将看到我最后的评论。马特发现了真正的问题。
    <ComboBox x:Name="ComboboxAccountType" 
        ItemsSource="{Binding DisplayAccountTypes}"
        SelectedItem="{Binding SelectedDisplayAccountType}"
        DisplayMemberPath="DisplayText"
        SelectedValuePath="Item" >