|
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
namespace WpfEnumBindingDemo
{
#region FontStyles enum
/// <summary>
/// Font styles enumeration.
/// </summary>
public enum FontStyles { Normal, Bold, Italic, BoldItalic }
#endregion
#region FontStylesHelper Class
/// <summary>
/// Helper class for the FontStylkes enum.
/// </summary>
/// <remarks>
/// This class provides a Dictionary object with friendly enum names
/// to the IValueConverters that support the FontStyles enum.
/// </remarks>
internal static class FontStylesHelper
{
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
static FontStylesHelper()
{
// Create forward (enum-to friendly name) dictionary
FontStyleFriendlyNames = new Dictionary<FontStyles, string>
{
{FontStyles.Normal, "Normal Style"},
{FontStyles.Bold, "Bold Style"},
{FontStyles.Italic, "Italic Style"},
{FontStyles.BoldItalic, "Bold + Italic Style"},
};
// Create reverse (friendly name-to-enum) dictionary
FontStyleEnumValues = FontStyleFriendlyNames.ToDictionary(x => x.Value, x => x.Key);
}
#endregion
#region Properties
/// <summary>
/// Returns font style friendly name for enum value passed in.
/// </summary>
public static Dictionary<FontStyles, String> FontStyleFriendlyNames { get; set; }
/// <summary>
/// Returns font style enum value for friendly name passed in.
/// </summary>
public static Dictionary<String, FontStyles> FontStyleEnumValues { get; set; }
#endregion
}
#endregion
#region FontStylesListProvider
public class FontStylesListProvider : IValueConverter
{
#region Implementation of IValueConverter
/// <summary>
/// Converts a value.
/// </summary>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
/// <param name="value">The value produced by the binding source.</param><param name="targetType">The type of the binding target property.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
/* Note that this converter does not convert a value passed in. Instead, it generates
* a list of user-friendly counterparts for each menber of the target enum and
* returns that list to the caller. */
var fontStyleList = FontStylesHelper.FontStyleFriendlyNames.Values.ToList();
return fontStyleList;
}
/// <summary>
/// Converts a value.
/// </summary>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
/// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
#endregion
#region FontStylesValueConverter
public class FontStylesValueConverter: IValueConverter
{
#region Implementation of IValueConverter
/// <summary>
/// Converts a value.
/// </summary>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
/// <param name="value">The value produced by the binding source.</param><param name="targetType">The type of the binding target property.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var enumValue = (FontStyles)value;
var friendlyName = FontStylesHelper.FontStyleFriendlyNames[enumValue];
return friendlyName;
}
/// <summary>
/// Converts a value.
/// </summary>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
/// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var friendlyName = (String)value;
var enumValue = FontStylesHelper.FontStyleEnumValues[friendlyName];
return enumValue;
}
#endregion
}
#endregion
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.