简介:
TypeConverter 提供一种将值的类型转换为其他类型以及访问标准值和子属性的统一方法。另一种转换参考如下链接: IValueConverter 。
介绍:
IValueConverter | 常用于数据绑定的转换,注重UI的实时更新。 |
TypeConverter | 常用于类型的转换,注重类型之间的变换。 |
使用:
Human类转换为string类型
[TypeConverter(typeof(HumanTypeConvert))]
public class Human
{
public string Name { set; get; }
public Human Child { get; set; }
}
public class HumanTypeConvert : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
Human h = new Human();
h.Name = value as string;
return h;
}
return base.ConvertFrom(context, culture, value);
}
}
Human h = (Human)this.FindResource("humanSour");
MessageBox.Show(h.Child.Name);
<Window.Resources>
<local:Human x:Key="humanSour" Child="Child"/>
</Window.Resources>