Click here to Skip to main content
15,884,473 members
Articles / Desktop Programming / WPF

An Editable ListBox for WPF

Rate me:
Please Sign up or sign in to vote.
4.83/5 (7 votes)
13 May 2009CPOL19 min read 78.2K   3K   23  
A simple to implement editable listbox for WPF.
using System;
using System.Drawing;
using System.ComponentModel;

namespace EditableListboxApp.Infrastructure
{
    public enum WeebitType
    {
        Standard = 1,
        Custom = 2
    }

    public class WeebitData : IDataErrorInfo
    {
        enum ValidationProperties
        {
            Name,
            Description,
            Length,
            Width,
            Height
        }

        #region properties
        public string Name
        {
            set;
            get;
        }
        public string Description
        {
            set;
            get;
        }
        public string Length
        {
            set;
            get;
        }
        public string Width
        {
            set;
            get;
        }
        public string Height
        {
            set;
            get;
        }
        public Color WeebitColor
        {
            get;
            set;
        }

        public WeebitType WeebitType
        {
            set;
            get;
        }
        #endregion
        public bool IsValid()
        {
            bool returnVal = true;
            foreach (string property in Enum.GetNames(typeof(ValidationProperties)))
                if (GetValidationError(property) != null)
                {
                    returnVal = false;
                    break;
                }
            return returnVal;
        }
        #region IDataErrorInfo

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion
        #region validations
        string GetValidationError(string propertyName)
        {
            string error = null;

            switch (propertyName)
            {
                case "Name":
                    error = this.ValidateName();
                    break;

                case "Description":
                    error = this.ValidateDescription();
                    break;
                case "Length":
                    error = this.ValidateFloat(this.Length,"Length");
                    break;
                case "Width":
                    error = this.ValidateFloat(this.Width,"Width");
                    break;
                case "Height":
                    error = this.ValidateFloat(this.Height,"Height");
                    break;
            }

            return error;
        }
        string ValidateName()
        {
            if (String.IsNullOrEmpty(this.Name))
            {
                return "Enter Name";
            }
            return null;
        }
        string ValidateDescription()
        {
            if (String.IsNullOrEmpty(this.Description))
            {
                return "Enter Description";
            }
            return null;
        }
        string ValidateFloat(string value, string item)
        {
            if (String.IsNullOrEmpty(value))
            {
                return "Enter "+item;
            }
            try
            {
                float temp = float.Parse(value);
                return null;
            }
            catch
            {
                return "Invalid entry.";
            }
        }

        #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)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions