WPF属性与特性的映射(TypeConverter)
2016-03-23 14:19
369 查看
1,定义一个类
public class Human
{
public string Name { get; set; }
public Human Child { get; set; }
}
2在XAML文件中引用
<Window.Resources>
<Local:Human x:Key="human" Child="明洋" x:Name="human"></Local:Human>
</Window.Resources>
3添加转换类
public class StringToHumanTypeConverter:TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.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);
}
}
4引用转化类
[TypeConverterAttribute(typeof(StringToHumanTypeConverter))]
//[TypeConverter(typeof(StringToHumanTypeConverter))]与上面的一样
public class Human
{
public string Name { get; set; }
public Human Child { get; set; }
}
5测试映射
private void Button_Click(object sender, RoutedEventArgs e)
{
Human h1 = (Human)this.FindResource("human");//对应X:Key
MessageBox.Show(h1.Child.Name);
}
public class Human
{
public string Name { get; set; }
public Human Child { get; set; }
}
2在XAML文件中引用
<Window.Resources>
<Local:Human x:Key="human" Child="明洋" x:Name="human"></Local:Human>
</Window.Resources>
3添加转换类
public class StringToHumanTypeConverter:TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.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);
}
}
4引用转化类
[TypeConverterAttribute(typeof(StringToHumanTypeConverter))]
//[TypeConverter(typeof(StringToHumanTypeConverter))]与上面的一样
public class Human
{
public string Name { get; set; }
public Human Child { get; set; }
}
5测试映射
private void Button_Click(object sender, RoutedEventArgs e)
{
Human h1 = (Human)this.FindResource("human");//对应X:Key
MessageBox.Show(h1.Child.Name);
}
相关文章推荐
- 百度前端技术学院-task1.3源代码
- python截图
- Android学习资源
- php异常处理笔记
- CentOS 6.5/6.6 安装mysql 5.7 最完整版教程
- Java BeanValidation
- Axure 安装及使用心得
- 使用CocoaPods时 显示updating local specs repositories 并且一直不动
- 6步骤实现CentOS系统环境精简优化
- Java文件File操作一:文件的创建和删除
- iOS 去掉导航栏的边界黑线&去掉搜索框的边界黑线和其中文本输入框的阴影 - 解决方案
- Linux常用命令
- C++:泛型编程(单词数)
- Python学习-机器学习实战-ch02KNN_part2
- LintCode 查找斐波纳契数列中第 N 个数
- Surface Shader
- RabbitMQ监控(5)——OpenCharts展示
- 将 Shiro 作为应用的权限基础 四:shiro的配置说明
- Centos7下搭建LAMP平台环境
- Spinner使用方法