我是靠谱客的博主 风中灯泡,这篇文章主要介绍TypeConverter使用,现在分享给大家,希望可以做个参考。

如下代码,

<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使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部