问题描述
我有一个提供将枚举绑定到组合框的操作的 MarkupExtension,这是我的标记
public class EnumBindingSourceExtension : MarkupExtension
{
private Type _enumType;
public Type EnumType
{
get { return this._enumType; }
set
{
if (value != this._enumType)
{
if (null != value)
{
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
if (!enumType.IsEnum)
throw new ArgumentException("Type must be for an Enum.");
}
this._enumType = value;
}
}
}
public EnumBindingSourceExtension() { }
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = Enum.GetValues(actualEnumType);
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType,enumValues.Length + 1);
enumValues.copyTo(tempArray,1);
return tempArray;
}
}
现在我想在更改程序语言时强制组合框检索其项目 可以看到现在的情况 第一张图,一切正常
但是在第二张图中,当程序的语言发生变化时,组合框项目不会发生变化,但它们的值(选定项目)会发生变化。
所以我正在寻找一种强制组合框更新的方法 我还使用以下代码来本地化枚举
public class LocalizedDescriptionAttribute : DescriptionAttribute
{
ResourceManager _resourceManager;
string _resourceKey;
public LocalizedDescriptionAttribute(string resourceKey,Type resourceType)
{
_resourceManager = new ResourceManager(resourceType);
_resourceKey = resourceKey;
}
public override string Description
{
get
{
string description = _resourceManager.GetString(_resourceKey);
return string.IsNullOrWhiteSpace(description) ? string.Format("[[{0}]]",_resourceKey) : description;
}
}
}
和用法:
<ComboBox ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:ContryEnum}}/>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)