Click here to Skip to main content
15,897,273 members
Articles / Desktop Programming / WPF

Data Binding Enums in WPF

Rate me:
Please Sign up or sign in to vote.
4.84/5 (8 votes)
23 Aug 2011CPOL9 min read 44.7K   689   24  
Binding enums to UI elements in WPF using friendly names
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Foresight Systems
United States United States
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.

Comments and Discussions