Click here to Skip to main content
15,891,529 members
Articles / Desktop Programming / XAML

Diagnostic Explorer

Rate me:
Please Sign up or sign in to vote.
4.93/5 (41 votes)
29 Nov 2010LGPL315 min read 82.5K   1.5K   120  
A .NET library and website which allows developers to expose and view arbitrary diagnostic information about their .NET processes.
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;

namespace ContextMenuControls
{
    [TemplateVisualState(Name = MenuItem.StateNormal, GroupName = MenuItem.GroupCommon)]
    [TemplateVisualState(Name = MenuItem.StateActive, GroupName = MenuItem.GroupCommon)]
    public sealed class MenuItem : MenuItemBase
    {
        public delegate void ClickHandler(object sender, EventArgs e);
        public event ClickHandler Click;
        
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MenuItem), null);
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(MenuItem), null);
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(MenuItem), null);

        private const string GroupCommon = "Common";
        private const string StateNormal = "Normal";
        private const string StateActive = "Active";        

        private bool _isActive;

        public MenuItem() : base()
        {
            this.DefaultStyleKey = typeof(MenuItem);
            _isActive = false;            
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            Focus();
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            _isActive = false;
            UpdateState(true);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            
            Focus();
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            _isActive = true;
            UpdateState(true);
        }

        protected override void OnLostFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            _isActive = false;
            UpdateState(true);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {            
            base.OnKeyDown(e);
            if (e.Key == Key.Enter || e.Key == Key.Space)
            {
                OnClick(new EventArgs());                
            }
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);

            OnClick(new EventArgs());
        }

        protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseRightButtonUp(e);

            OnClick(new EventArgs());
        }

        private void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        private void UpdateState(bool useTransitions)
        {
            if (_isActive)
            {
                VisualStateManager.GoToState(this, StateActive, useTransitions);
            }
            else
            {
                VisualStateManager.GoToState(this, StateNormal, useTransitions);
            }
        }    

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                SetValue(CommandProperty, value);
            }
        }

        public object CommandParameter
        {
            get
            {
                return GetValue(CommandParameterProperty);
            }
            set
            {
                SetValue(CommandParameterProperty, value);
            }
        }
    }   
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
United Kingdom United Kingdom
I am a software developer originally from Auckland, New Zealand. I have lived and worked in London since 2005.

Comments and Discussions