Click here to Skip to main content
15,888,579 members
Articles / Programming Languages / C#

Developing a MultiSelector

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
3 Dec 2009BSD15 min read 65.3K   1.3K   45  
A MultiSelector-derived control that supports multiple selection modes, scoped selection, customizable lasso-tool and selection logic
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace SelectionAreaSample
{
    public sealed class SelectionAreaItem : ContentControl
    {
        #region Public Events

        public static readonly RoutedEvent SelectedEvent = Selector.SelectedEvent.AddOwner(typeof(SelectionAreaItem));

        public event RoutedEventHandler Selected
        {
            add
            {
                base.AddHandler(SelectedEvent, value);
            }
            remove
            {
                base.RemoveHandler(SelectedEvent, value);
            }
        }

        public static readonly RoutedEvent UnselectedEvent = Selector.UnselectedEvent.AddOwner(typeof(SelectionAreaItem));

        public event RoutedEventHandler Unselected
        {
            add
            {
                base.AddHandler(UnselectedEvent, value);
            }
            remove
            {
                base.RemoveHandler(UnselectedEvent, value);
            }
        }

        #endregion Public Events

        #region Public Properties

        public static readonly DependencyProperty IsSelectedProperty = Selector.IsSelectedProperty.AddOwner(typeof(SelectionAreaItem), new FrameworkPropertyMetadata(false, OnIsSelectedChanged));

        public bool IsSelected
        {
            get
            {
                return (bool)GetValue(IsSelectedProperty);
            }
            set
            {
                SetValue(IsSelectedProperty, value);
            }
        }

        private static void OnIsSelectedChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            var item = target as SelectionAreaItem;

            if ((bool)e.NewValue)
            {
                item.RaiseEvent(new RoutedEventArgs(Selector.SelectedEvent, item));
            }
            else
            {
                item.RaiseEvent(new RoutedEventArgs(Selector.UnselectedEvent, item));
            }
        }

        #endregion Public Properties

        #region Internal Properties

        internal SelectionArea ParentSelectionArea
        {
            get
            {
                return ItemsControl.ItemsControlFromItemContainer(this) as SelectionArea;
            }
        }

        #endregion Internal Properties

        #region Protected Methods

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

            var parent = ParentSelectionArea;

            if (parent != null)
            {
                parent.NotifyItemClicked(this);
            }

            e.Handled = true;
        }

        protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
        {
            if (VisualTreeHelper.GetDescendantBounds(this).Contains(hitTestParameters.HitPoint))
            {
                return new PointHitTestResult(this, hitTestParameters.HitPoint);
            }

            return null;
        }

        protected override GeometryHitTestResult HitTestCore(GeometryHitTestParameters hitTestParameters)
        {
            var geometry = new RectangleGeometry(VisualTreeHelper.GetDescendantBounds(this));
            return new GeometryHitTestResult(this, geometry.FillContainsWithDetail(hitTestParameters.HitGeometry));
        }

        #endregion Protected 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 BSD License


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions