WPF中 propertyGrid控件 实现多语言版本问题 多语言直接resource里面定义多个语言文件 根据选择语言不同加载不同resource文件不就行了然后用的地方findresource(“key”)不就行了么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在别的地方是可以的 但是findresource(“key”)在替换上面的文字的时候会报错,要求是常量。 什么叫改变后面的value 后台的值不会改变???另外我看那个方法只是在玩,根本不能用在实际项目中,因为这样的处理没有任何实际意义。 利用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; } } 我的问题解决了,参考这个例子http://www.codeproject.com/Articles/4341/Globalized-Property-Grid-Revisited可以实现语言的动态切换,但是CategoryName无法改变。查了MSDN文件中的CategoryAttribute.GetLocalizedString Method,说" Category 属性第一次被存取时会呼叫这个方法来查阅指定分类的当地语系化名称。",就是说GetLocalizedString Method只能执行一次。不知道能不能强制改变。不行的话就加个重启后生效。。 哈哈,我正好路过,楼主问题解决了吧。在 PropertyDescriptor 重写 Catagory属性,即可达到 目录多语言的 热切换。 我的OleDbDataReader.Read()方法读不到数据? Dll注册问题 [!急!]来个高手:正则表达式关于网页标记.属性.匹配的问题... 怎样在c#中调用c++的静态方法 这个机能 怎么用c#实现?? 急~~ C#怎么实现函数开始和暂停 不同分辨率下的窗体控件位置的问题 事件的写法是不是错了 上一贴中提到接收的串口数据并写入Excel的完整程序 如何调用另一个方法,也就是方法重载的问题. foreach循环里面的变量声明,不能覆盖外部的变量声明,为什么? 做了个简单的小游戏和一个简单的播放器,但是按键冲突,怎么解决啊。谢谢。。内附截图
在别的地方是可以的 但是findresource(“key”)在替换上面的文字的时候会报错,要求是常量。
另外我看那个方法只是在玩,根本不能用在实际项目中,因为这样的处理没有任何实际意义。
{
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;
}
}
我的问题解决了,参考这个例子
http://www.codeproject.com/Articles/4341/Globalized-Property-Grid-Revisited
可以实现语言的动态切换,但是CategoryName无法改变。查了MSDN文件中的CategoryAttribute.GetLocalizedString Method,说" Category 属性第一次被存取时会呼叫这个方法来查阅指定分类的当地语系化名称。",就是说GetLocalizedString Method只能执行一次。不知道能不能强制改变。
不行的话就加个重启后生效。。
在 PropertyDescriptor 重写 Catagory属性,即可达到 目录多语言的 热切换。