sportdog

导航

 
  1. StringToHumanTypeConverter类(从TypeConverter继承
  2. using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WpfStudy2018
    {
        /// <summary>
        /// 将string类型转成Human
        /// </summary>
        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.ToString();
                    return h;
                }
                
                return base.ConvertFrom(context, culture, value);
            }
        }
    }
    

      

  3. Human类
  4.  [TypeConverterAttribute(typeof(StringToHumanTypeConverter))]
        public class Human
        {
            public string Name
            {
                get;
                set;
            }
    
            public Human Child
            {
                get;
                set;
            }
        }

     

  5. 前端引用: xmlns:local="clr-namespace:WpfStudy2018"
  6. 前段代码:
    <Window.Resources>
            <local:Human x:Key="Snow" Name="zhouyg" Child="Jack"></local:Human>
      </Window.Resources>

     

  7. 后台代码:Human h =   this.FindResource("Snow") as Human;
posted on 2018-03-07 14:23  sportdog  阅读(184)  评论(0编辑  收藏  举报