Click here to Skip to main content
15,895,777 members
Articles / Desktop Programming / XAML

Silverlight Menu4U

Rate me:
Please Sign up or sign in to vote.
4.90/5 (59 votes)
14 Aug 2011CPOL11 min read 120.6K   4.1K   85  
A new Silverlight menu with styling and templating.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Markup;
using System.Collections.Specialized;

namespace Silverlight.Controls
{
    [ContentProperty("Children")]
    public class MenuItem : ObservableCollection<MenuItem>, INotifyPropertyChanged
    {
        #region private
        bool isEnabled = true;
        bool isCheckable = false;
        bool isChecked = false;
        string text = "";
        ObservableCollection<MenuItem> children = new ObservableCollection<MenuItem>();
        PropertyChangedEventHandler handler;
        #endregion

        #region public
        public event PropertyChangedEventHandler PropertyChanged;
        public String Description { get; set; }
        public String IconUrl { get; set; }
        public String ShortCut { get; set; }
        public ObservableCollection<MenuItem> Children
        {
            get
            {
                return children;
            }
            set
            {
                children = new ObservableCollection<MenuItem>();
                foreach (MenuItem item in value)
                {
                    children.Add(item);
                    Items.Add(item);
                }
                NotifyPropertyChanged("Children");
            }
        }

        public MenuItem Self
        {
            get {return this;}
        }

        public ICommand Command { get; set; }
        public string ImageSource { get; set; }
        public bool IsSeparator { get; private set; }

        public string Text
        {
            get { return this.text; }

            set
            {
                if (value != this.text)
                {
                    this.text = value;

                    if (value.Equals("-"))
                        IsSeparator = true;

                    NotifyPropertyChanged("Text");
                }
            }
        }

        public bool IsEnabled
        {
            get { return this.isEnabled; }

            set
            {
                if (value != this.isEnabled)
                {
                    this.isEnabled = value;
                    NotifyPropertyChanged("IsEnabled");
                }
            }
        }

        public bool IsChecked
        {
            get { return this.isChecked; }

            set
            {
                if (value != this.isChecked)
                {
                    this.isChecked = value;
                    NotifyPropertyChanged("IsChecked");
                }
            }
        }

        public bool IsCheckable
        {
            get { return this.isCheckable; }

            set
            {
                if (value != this.isCheckable)
                {
                    this.isCheckable = value;
                    NotifyPropertyChanged("IsCheckable");
                }
            }
        }

        private void NotifyPropertyChanged(String propertyName)
        {
            handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region Ctor
        public MenuItem()
        {
            this.isEnabled = true;
        }

        public MenuItem(string item)
        {
            Text = item;
            Children = new ObservableCollection<MenuItem>();
            IsCheckable = 
            IsChecked = false;
        }

        public MenuItem(string item, string iconUrl)
        {
            Text = item;
            IconUrl = iconUrl;
            Children = new ObservableCollection<MenuItem>();
            IsCheckable =
            IsChecked = false;
        }

        public MenuItem(string item, string iconUrl, ICommand command)
        {
            Text = item;
            IconUrl = iconUrl;
            Children = new ObservableCollection<MenuItem>();
            Command = command;
            IsCheckable =
            IsChecked = false;
        }

        public MenuItem(string item, ICommand command)
        {
            Text = item;
            Children = new ObservableCollection<MenuItem>();
            Command = command;
            IsCheckable =
            IsChecked = false;
        }

        #endregion

        #region methods

        public override string ToString()
        {
            return Text;
        }

        #endregion methods
    }
}

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
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions