Никита
6 years ago
7 changed files with 103 additions and 25 deletions
@ -0,0 +1,52 @@ |
|||
using System; |
|||
using System.Windows.Markup; |
|||
|
|||
namespace eCompanies |
|||
{ |
|||
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; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Reflection; |
|||
|
|||
namespace eCompanies |
|||
{ |
|||
public class EnumDescriptionTypeConverter : EnumConverter |
|||
{ |
|||
public EnumDescriptionTypeConverter(Type type) |
|||
: base(type) |
|||
{ |
|||
} |
|||
|
|||
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) |
|||
{ |
|||
if (destinationType == typeof(string)) |
|||
{ |
|||
if (value != null) |
|||
{ |
|||
FieldInfo fi = value.GetType().GetField(value.ToString()); |
|||
if (fi != null) |
|||
{ |
|||
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); |
|||
return ((attributes.Length > 0) && (!String.IsNullOrEmpty(attributes[0].Description))) ? attributes[0].Description : value.ToString(); |
|||
} |
|||
} |
|||
|
|||
return string.Empty; |
|||
} |
|||
|
|||
return base.ConvertTo(context, culture, value, destinationType); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue