C# 解决方法,以避免;无法创建默认转换器以执行';双向';类型之间的转换';派生的';和';基数'&引用;错误
我有一些类型的层次结构:C# 解决方法,以避免;无法创建默认转换器以执行';双向';类型之间的转换';派生的';和';基数'&引用;错误,c#,wpf,C#,Wpf,我有一些类型的层次结构: 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 IEnumer
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
}
XAML通常看起来像:
<myCtrls:BaseSelector ItemsSource="{Binding SomeItems}"
SelectedItem="{Binding SelectedItem}"/>
用于绑定:
<myCtrls:BaseSelector ItemsSource="{Binding SomeItems}"
SelectedItem="{Binding SelectedItem, Converter={StaticResource DummyConverterKey}}"/>
但我根本不想使用它——正如您所看到的,转换器中没有任何有效负载(虽然有很多这样的属性)
还有其他解决方法吗?目前,我已经解决了一个问题,将用户控件属性的属性类型分别替换为
IEnumerable
/object
(IEnumerable
/object
也是一个解决方案):
这会导致在用户控件内部进行额外的类型检查,但不会产生任何绑定错误(我真的不明白,为什么-
object
的情况与IMO中的Base
相同)。对类使用TypeConverter以避免此错误:
[TypeConverter(typeof(MyConverter))]
public class Derived_N : Base
{
}
类型转换器:
public class MyConverter: TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
return sourceType == typeof(Base);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
return value as Base;
}
public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
{
return destinationType == typeof(Base);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destinationType)
{
return value == null ? null : value as Base;
}
}
PS:请根据需要更改上述代码。我只是写了最小值以防止出错。你不能使用UIPropertyMetadata吗?@KyloRen:是的,当然。那就是你的解决方案。使用UIPropertyMetadata,您将无法获得警告。我应该把这个作为答案吗?@KyloRen:哦,对不起。我已经更仔细地查看了
UIPropertyMetadata
,我确实需要FrameworkPropertyMetadata选项。默认情况下,在每个绑定中手动设置此选项将是一件痛苦的事情。您在运行时见过绑定吗。我用UIPropertyMetadata创建了一个示例,默认情况下它是双向的。(用于我的依赖项属性)
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));
[TypeConverter(typeof(MyConverter))]
public class Derived_N : Base
{
}
public class MyConverter: TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
return sourceType == typeof(Base);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
return value as Base;
}
public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
{
return destinationType == typeof(Base);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destinationType)
{
return value == null ? null : value as Base;
}
}