如下代码,
<Window.Resources>
<local:Human x:Key="human" Name="Tester1" Child="ChildOfTester1"/>
</Window.Resources>
class Human
{
public string Name{get;set;}
public Human Child{get;set;}
}
为了让以上代码工作,则必须提供一个类型转换, 从string转到Human。
步骤如下,
1. 定义StringToHumanConverter : TypeConverter, 重写其ConvertFrom方法,
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
return new Human(value as string);
}
return base.ConvertFrom(context, culture, value);
}
2. 附加到Human类,
[TypeConverter(typeof(StringToHumanConverter))]
class Human
Done.
最后
以上就是风中灯泡最近收集整理的关于TypeConverter使用的全部内容,更多相关TypeConverter使用内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复