gpt4 book ai didi

c# - 避免 "Cannot create default converter to perform ' 类型 'Derived_N' 和 'Base' 之间的双向“转换”错误的解决方法

转载 作者:行者123 更新时间:2023-11-30 12:42:01 28 4
gpt4 key购买 nike

我有一些类型层次结构:

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

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

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}"/>

这按预期工作,但存在如下绑定(bind)错误:

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;
}
}

用于绑定(bind):

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

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

关于c# - 避免 "Cannot create default converter to perform ' 类型 'Derived_N' 和 'Base' 之间的双向“转换”错误的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35266870/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com