c# – 解决方法以避免“无法创建默认转换器以在类型’Derived_N’和’Base’之间执行’双向’转换”错误

我有一些类型层次结构:

public class Base {}
public class Derived_1 : Base {}
public class Derived_2 : Base {}
// more descendants...
public class Derived_N : Base {}

此层次结构中的类型用作视图模型中的查找列表:

public class SomeViewModel
{
    // available items
    public IEnumerable<Derived_N> SomeItems { get; }

    // currently selected item
    public Derived_N SelectedItem { get; set; }

    // there could be several property pairs as above
}

要从查找列表中选择值,我已经创建了用户控件(某种选择器).从选择过程开始,所有Base后代看起来都相似,用户控件操作Base类型属性:

    public IEnumerable<Base> ItemsSource
    {
        get { return (IEnumerable<Base>)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IEnumerable<Base>), typeof(BaseSelector), new PropertyMetadata(null));

    public Base SelectedItem
    {
        get { return (Base)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(Base), typeof(BaseSelector), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

XAML通常看起来像:

<myCtrls:BaseSelector ItemsSource="{Binding SomeItems}"
                      SelectedItem="{Binding SelectedItem}"/>

这按预期工作,但有这样的绑定错误:

Cannot create default converter to perform ‘two-way’ conversions
between types ‘Derived_N’ and ‘Base’

我知道,为什么它们在输出窗口中 – 理论上,SelectedItem可以是任何类型,派生自Base,但实际上这不是我的情况.
如果这个转换器错误消失:

public class DummyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

用于绑定:

<myCtrls:BaseSelector ItemsSource="{Binding SomeItems}"
                      SelectedItem="{Binding SelectedItem, Converter={StaticResource DummyConverterKey}}"/>

但我根本不想使用它 – 正如你所看到的那样,转换器中没有任何有效载荷(虽然有很多这样的属性).

还有其他解决方法吗?

最佳答案 现在,我已经解决了将用户控件属性的属性类型分别替换为IEnumerable / object的问题(IEnumerable< object> / object也是一个解决方案):

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(BaseSelector), new PropertyMetadata(null));

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(object), typeof(BaseSelector), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

这导致在用户控件内进行额外的类型检查,但不会产生任何绑定错误(我真的不明白,为什么 – 对象的情况与Base,IMO相同).

点赞