加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 解决方法以避免“无法创建默认转换器以在类型’Derived_N

发布时间:2020-12-15 22:26:46 所属栏目:百科 来源:网络整理
导读:我有一些类型层次结构: public class Base {}public class Derived_1 : Base {}public class Derived_2 : Base {}// more descendants...public class Derived_N : Base {} 此层次结构中的类型用作视图模型中的查找列表: public class SomeViewModel{ // a
我有一些类型层次结构:

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),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,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),new PropertyMetadata(null));

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读