Browse Source

LocalizableDescriptionAttribute and EnumToFriendlyNameConverter to display enum's in a user friendly name

pull/20/merge
PeterForstmeier 14 years ago
parent
commit
89528ed6e8
  1. 68
      src/AddIns/Analysis/CodeQuality/Src/Utility/LocalizeableCombo/EnumToFriendlyNameConverter.cs
  2. 114
      src/AddIns/Analysis/CodeQuality/Src/Utility/LocalizeableCombo/LocalizableDescriptionAttribute.cs

68
src/AddIns/Analysis/CodeQuality/Src/Utility/LocalizeableCombo/EnumToFriendlyNameConverter.cs

@ -0,0 +1,68 @@ @@ -0,0 +1,68 @@
/*
* Created by SharpDevelop.
* User: Peter Forstmeier
* Date: 19.09.2011
* Time: 19:37
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Data;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
namespace ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo
{
/// <summary>
/// Description of EnumToFriendlyNameConverter.
/// http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx
/// </summary>
[ValueConversion(typeof(object), typeof(String))]
public class EnumToFriendlyNameConverter : IValueConverter
{
#region IValueConverter implementation
/// <summary>
/// Convert value for binding from source object
/// </summary>
public object Convert(object value, System.Type targetType,
object parameter, CultureInfo culture)
{
// To get around the stupid WPF designer bug
if (value != null)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
// To get around the stupid WPF designer bug
if (fi != null)
{
var attributes =
(LocalizableDescriptionAttribute[])
fi.GetCustomAttributes(typeof
(LocalizableDescriptionAttribute), false);
return ((attributes.Length > 0) &&
(!String.IsNullOrEmpty(attributes[0].Description)))
?
attributes[0].Description
: value.ToString();
}
}
return string.Empty;
}
/// <summary>
/// ConvertBack value from binding back to source object
/// </summary>
public object ConvertBack(object value, System.Type targetType,
object parameter, CultureInfo culture)
{
throw new Exception("Cant convert back");
}
#endregion
}
}

114
src/AddIns/Analysis/CodeQuality/Src/Utility/LocalizeableCombo/LocalizableDescriptionAttribute.cs

@ -0,0 +1,114 @@ @@ -0,0 +1,114 @@
/*
* Created by SharpDevelop.
* User: Peter Forstmeier
* Date: 19.09.2011
* Time: 19:36
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.ComponentModel;
namespace ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo
{
/// <summary>
/// Description of LocalizableDescriptionAttribute.
/// http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx
/// </summary>
///
[AttributeUsage(AttributeTargets.All,Inherited = false,AllowMultiple = true)]
public sealed class LocalizableDescriptionAttribute : DescriptionAttribute
{
#region Public methods.
// ------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the
/// <see cref="LocalizableDescriptionAttribute"/> class.
/// </summary>
/// <param name="description">The description.</param>
/// <param name="resourcesType">Type of the resources.</param>
public LocalizableDescriptionAttribute
(string description,System.Type resourcesType) : base(description)
{
// _resourcesType = resourcesType;
}
public LocalizableDescriptionAttribute (string description) : base(description)
{
// _resourcesType = resourcesType;
}
#endregion
#region Public properties.
/// <summary>
/// Get the string value from the resources.
/// </summary>
/// <value></value>
/// <returns>The description stored in this attribute.</returns>
/*
public override string Description
{
get
{
if (!_isLocalized)
{
ResourceManager resMan =
_resourcesType.InvokeMember(
@"ResourceManager",
BindingFlags.GetProperty | BindingFlags.Static |
BindingFlags.Public | BindingFlags.NonPublic,
null,
null,
new object[] { }) as ResourceManager;
CultureInfo culture =
_resourcesType.InvokeMember(
@"Culture",
BindingFlags.GetProperty | BindingFlags.Static |
BindingFlags.Public | BindingFlags.NonPublic,
null,
null,
new object[] { }) as CultureInfo;
_isLocalized = true;
if (resMan != null)
{
DescriptionValue =
resMan.GetString(DescriptionValue, culture);
}
}
return DescriptionValue;
}
}
*/
public override string Description
{
get
{
if (!_isLocalized)
{
_isLocalized = true;
DescriptionValue = Description;
}
return DescriptionValue;
}
}
#endregion
#region Private variables.
// private readonly Type _resourcesType;
private bool _isLocalized;
#endregion
}
}
Loading…
Cancel
Save