首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WPF中 propertyGrid控件 实现多语言版本有关问题

2013-09-05 
WPF中 propertyGrid控件 实现多语言版本问题propertyGrid 要设置多语言 而且要绑定数据(即在propertyGrid

WPF中 propertyGrid控件 实现多语言版本问题
propertyGrid 要设置多语言 而且要绑定数据(即在propertyGrid中修改一个字段后台字段的值也修改)
你知道解决方案吗? 求解答
1、首先[CategoryAttribute("标题1"),
        DisplayName("标题2"),
        Description(""),
        DefaultValue(typeof(string), "")
        ]
        public string Name
是不行的,CategoryAttribute要求输入常量,就没办法加入语言控制。
2、网上有些重写这个控件的方法可以实现语言控制,但是用这个方法改变后面的value 后台的值不会改变。实现方法:
http://hi.baidu.com/knowaysoft/item/1f32cbe313267011595dd826

有没有办法实现数据的绑定,我看里面重写了以后有数据的get set,但是没办法把改变传回后台代码。有没有了解的大神。。。。
[解决办法]
多语言直接resource里面定义多个语言文件 根据选择语言不同加载不同resource文件不就行了
然后用的地方findresource(“key”)不就行了么?
[解决办法]
利用TypeDescriptor.AddProvider方法,添加运行时的TypeDescriptionProvider,下面是对IDictionary<string, object>类型定义的TypeDescriptionProvider,参照这个例子,给你的类型定义自己的TypeDescriptionProvider,添加多语言的CategoryAttribute值。


    public class DynamicDescriptionProvider : TypeDescriptionProvider
    {
        public DynamicDescriptionProvider() : base() { }

        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
        {
            return new DynamicTypeDescriptor(objectType, instance);
        }
    }
    public class DynamicTypeDescriptor : CustomTypeDescriptor
    {
        public DynamicTypeDescriptor(Type objectType, object instance)
            : base()
        {
            if (instance != null)


            {
                var tmp = (IDictionary<string, object>)instance;
                var names = tmp.Keys;
                foreach (var name in names)
                {
                    customFields.Add(new DynamicPropertyDescriptor(name, instance));
                }
            }
        }
        List<PropertyDescriptor> customFields = new List<PropertyDescriptor>();
        public override PropertyDescriptorCollection GetProperties()
        {
            return new PropertyDescriptorCollection(customFields.ToArray());
        }

        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return new PropertyDescriptorCollection(customFields.ToArray());
        }
    }
    public class DynamicPropertyDescriptor : PropertyDescriptor
    {
        Type propertyType = typeof(object);
        public DynamicPropertyDescriptor(string name, object instance)
            : base(name, null)
        {
            var obj = (IDictionary<string, object>)instance;
            if (obj[name] != null)
                propertyType = obj[name].GetType();
        }



        public override bool CanResetValue(object component)
        {
            return false;
        }

        public override Type ComponentType
        {
            get
            {
                return typeof(FastExpando);
            }
        }

        public override object GetValue(object component)
        {
            IDictionary<string, object> obj = (IDictionary<string, object>)component;
            return obj[Name];
        }

        public override bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public override Type PropertyType
        {
            get
            {
                return propertyType;
            }
        }

        public override void ResetValue(object component)
        {
            throw new NotImplementedException();
        }

        public override void SetValue(object component, object value)


        {
            IDictionary<string, object> obj = (IDictionary<string, object>)component;
            obj[Name] = value;
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }
    }

热点排行