Click here to Skip to main content
15,886,701 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 120K   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;

namespace Silverlight.Controls
{
    public class CustomListBox : ListBox
    {
        public delegate void CustomSelectionChangedDelegate(CustomListBox parentListBox, IEnumerable item, IEnumerable itemsSource, double stackedPosition, double width, double height);
        public delegate void ItemMouseLeftButtonDownDelegate(CustomListBox parentListBox, object item);

        public event CustomSelectionChangedDelegate CustomSelectionChanged;
        public event ItemMouseLeftButtonDownDelegate ItemMouseLeftButtonDown;

        Visibility headerVisibility = Visibility.Visible;

        Orientation orientation { get; set; }
        public int Level { get; private set; }
        public double HeaderHeight { get; private set; }
        public Visibility HeaderVisibility
        {
            get
            {
                return headerVisibility;
            }
            set
            {
                headerVisibility = value;
            }
        }

        public CustomListBox(int level, Orientation orientation)
        {
            this.SelectionChanged += new SelectionChangedEventHandler(CustomListBox_SelectionChanged);
            this.orientation = orientation;
            this.Level = level;
            this.HeaderVisibility = System.Windows.Visibility.Collapsed;
            this.HeaderHeight = 0;
            this.MouseEnter += new MouseEventHandler(CustomListBox_MouseEnter);
        }

        void CustomListBox_MouseEnter(object sender, MouseEventArgs e)
        {
            for (var i = 0; i < this.Items.Count; i++)
            {
                var container = (ListBoxItem)this.ItemContainerGenerator.ContainerFromIndex(i);
                if (container != null)
                {
                    container.MouseEnter -= new MouseEventHandler(container_MouseEnter);
                    container.MouseEnter += new MouseEventHandler(container_MouseEnter);

                    container.MouseMove -= new MouseEventHandler(container_MouseMove);
                    container.MouseMove += new MouseEventHandler(container_MouseMove);
                }
            }            
        }

        void container_MouseMove(object sender, MouseEventArgs e)
        {
            SelectItem(sender);
        }

        void container_MouseEnter(object sender, MouseEventArgs e)
        {
            SelectItem(sender);
        }

        private void SelectItem(object sender)
        {
            var container = (ListBoxItem)sender;
            dynamic item = this.ItemContainerGenerator.ItemFromContainer(container);
            var isEnabled = item.IsEnabled == null ? true : item.IsEnabled;
            var isSeparator = item.IsSeparator == null ? false : item.IsSeparator;

            if (isEnabled && !isSeparator)
            {
                if (this.SelectedItem != item)
                    this.SelectedItem = item;
            }
        }

        void CustomListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IEnumerable itemsSource = null;
            Point nwPoint = new Point(0, 0);
            Point nePoint = new Point(0, 0);
            Point swPoint = new Point(0, 0);
            Point sePoint = new Point(0, 0);

            var stackedPosition = 0.0;
            var width = 0.0;
            var height = 0.0;
            ListBoxItem container = null;

            if (this.SelectedItem != null)
            {
                itemsSource = ((MenuItem)this.SelectedItem).Children;

                for (var i = 0; i <= this.SelectedIndex; i++)
                {
                    container = (ListBoxItem)this.ItemContainerGenerator.ContainerFromIndex(i);
                    var child = (FrameworkElement)VisualTreeHelper.GetChild(container, 0);
                    child.MouseLeftButtonDown -= new MouseButtonEventHandler(child_MouseLeftButtonDown);
                    child.MouseLeftButtonDown += new MouseButtonEventHandler(child_MouseLeftButtonDown);

                    width = (double)container.GetValue(Canvas.ActualWidthProperty);
                    height = (double)container.GetValue(Canvas.ActualHeightProperty);

                    if (i < this.SelectedIndex)
                    {
                        if (this.orientation == Orientation.Vertical)
                        {
                            stackedPosition += height;
                        }
                        else
                        {
                            stackedPosition += width;
                        }
                    }
                }

                //dynamic item = this.ItemContainerGenerator.ItemFromContainer(container);
                MenuItem item = (MenuItem)this.ItemContainerGenerator.ItemFromContainer(container);
                var isEnabled = item.IsEnabled == null ? true : item.IsEnabled;

                if (isEnabled)
                {
                    if (CustomSelectionChanged != null)
                        CustomSelectionChanged(this, item, item.Children, stackedPosition, width, height);
                }
                else
                {
                    this.SelectedIndex = -1;
                }
            }
        }

        void child_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var container = (FrameworkElement)VisualTreeHelper.GetParent((FrameworkElement)sender);
            dynamic item = this.ItemContainerGenerator.ItemFromContainer(container);

            var isEnabled = item.IsEnabled == null ? true : item.IsEnabled;

            if (isEnabled)
            {
                MenuItem menuItem = (MenuItem)item;
                if (menuItem.Command != null)
                {
                    if (menuItem.Command.CanExecute(null))
                    {
                        menuItem.Command.Execute(null);
                    }
                }

                if (ItemMouseLeftButtonDown != null)
                    ItemMouseLeftButtonDown(this, item);
            }
        }
    }
}

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