Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / WPF

WPF Explorer Bar

Rate me:
Please Sign up or sign in to vote.
4.67/5 (21 votes)
5 Oct 2008MIT3 min read 106.6K   2.6K   85  
A WPF explorer bar implementation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.IO;
using System.Xml;

namespace Odyssey.Controls
{
    /// <summary>
    /// An Expander with animation.
    /// </summary>
    public class OdcExpander : HeaderedContentControl
    {
        static OdcExpander()
        {
            MarginProperty.OverrideMetadata(
                typeof(OdcExpander),
                new FrameworkPropertyMetadata(new Thickness(10, 10, 10, 2)));

            FocusableProperty.OverrideMetadata(typeof(OdcExpander),
                new FrameworkPropertyMetadata(false));

            DefaultStyleKeyProperty.OverrideMetadata(typeof(OdcExpander),
                new FrameworkPropertyMetadata(typeof(OdcExpander)));
        }

        /// <summary>
        /// Gets or sets the custom skin for the control.
        /// </summary>
        public static string Skin { get; set; }


        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            ApplySkin();
        }

        public void ApplySkin()
        {
            if (!string.IsNullOrEmpty(Skin))
            {
                Uri uri = new Uri(Skin, UriKind.Absolute);
                ResourceDictionary skin = new ResourceDictionary();
                skin.Source = uri;
                this.Resources = skin;
            }
        }

        public Brush HeaderBorderBrush
        {
            get { return (Brush)GetValue(HeaderBorderBrushProperty); }
            set { SetValue(HeaderBorderBrushProperty, value); }
        }

        public static readonly DependencyProperty HeaderBorderBrushProperty =
            DependencyProperty.Register("HeaderBorderBrush",
            typeof(Brush), typeof(OdcExpander), new UIPropertyMetadata(Brushes.Gray));

        public Brush HeaderBackground
        {
            get { return (Brush)GetValue(HeaderBackgroundProperty); }
            set { SetValue(HeaderBackgroundProperty, value); }
        }

        public static readonly DependencyProperty HeaderBackgroundProperty =
            DependencyProperty.Register("HeaderBackground",
            typeof(Brush), typeof(OdcExpander), new UIPropertyMetadata(Brushes.Silver));


        public bool IsMinimized
        {
            get { return (bool)GetValue(MinimizedProperty); }
            set { SetValue(MinimizedProperty, value); }
        }

        public static readonly DependencyProperty MinimizedProperty =
            DependencyProperty.Register("IsMinimized",
            typeof(bool), typeof(OdcExpander),
            new UIPropertyMetadata(false, IsMinimizedChanged));

        public static void IsMinimizedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OdcExpander expander = d as OdcExpander;
            RoutedEventArgs args = new RoutedEventArgs((bool)e.NewValue ? MinimizedEvent : MaximizedEvent);
            expander.RaiseEvent(args);
        }


        /// <summary>
        /// Gets or sets the ImageSource for the image in the header.
        /// </summary>
        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(OdcExpander), new UIPropertyMetadata(null));



        public bool IsExpanded
        {
            get { return (bool)GetValue(IsExpandedProperty); }
            set { SetValue(IsExpandedProperty, value); }
        }

        public event RoutedEventHandler Expanded
        {
            add { AddHandler(ExpandedEvent, value); }
            remove { RemoveHandler(ExpandedEvent, value); }
        }

        public event RoutedEventHandler Collapsed
        {
            add { AddHandler(CollapsedEvent, value); }
            remove { RemoveHandler(CollapsedEvent, value); }
        }

        public event RoutedEventHandler Minimized
        {
            add { AddHandler(MinimizedEvent, value); }
            remove { RemoveHandler(MinimizedEvent, value); }
        }

        public event RoutedEventHandler Maximized
        {
            add { AddHandler(MaximizedEvent, value); }
            remove { RemoveHandler(MaximizedEvent, value); }
        }

        #region dependency properties and routed events definition

        public static readonly DependencyProperty IsExpandedProperty =
            DependencyProperty.Register(
            "IsExpanded",
            typeof(bool),
            typeof(OdcExpander),
            new UIPropertyMetadata(true, IsExpandedChanged));


        public static void IsExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OdcExpander expander = d as OdcExpander;
            RoutedEventArgs args = new RoutedEventArgs((bool)e.NewValue ? ExpandedEvent : CollapsedEvent);
            expander.RaiseEvent(args);
        }

        public static readonly RoutedEvent ExpandedEvent = EventManager.RegisterRoutedEvent(
            "ExpandedEvent",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(OdcExpander));


        public static readonly RoutedEvent CollapsedEvent = EventManager.RegisterRoutedEvent(
            "CollapsedEvent",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(OdcExpander));

        public static readonly RoutedEvent MinimizedEvent = EventManager.RegisterRoutedEvent(
             "MinimizedEvent",
             RoutingStrategy.Bubble,
             typeof(RoutedEventHandler),
             typeof(OdcExpander));


        public static readonly RoutedEvent MaximizedEvent = EventManager.RegisterRoutedEvent(
            "MaximizedEvent",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(OdcExpander));

        #endregion


        /// <summary>
        /// Gets or sets the corner radius for the header.
        /// </summary>
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(OdcExpander), new UIPropertyMetadata(null));



        /// <summary>
        /// Gets or sets the background color of the header on mouse over.
        /// </summary>
        public Brush MouseOverHeaderBackground
        {
            get { return (Brush)GetValue(MouseOverHeaderBackgroundProperty); }
            set { SetValue(MouseOverHeaderBackgroundProperty, value); }
        }

        public static readonly DependencyProperty MouseOverHeaderBackgroundProperty =
            DependencyProperty.Register("MouseOverHeaderBackground", typeof(Brush), typeof(OdcExpander), new UIPropertyMetadata(null));


        /// <summary>
        /// Gets whether the PressedBackground is not null.
        /// </summary>
        public bool HasPressedBackground
        {
            get { return (bool)GetValue(HasPressedBackgroundProperty); }
            set { SetValue(HasPressedBackgroundProperty, value); }
        }

        public static readonly DependencyProperty HasPressedBackgroundProperty =
            DependencyProperty.Register("HasPressedBackground", typeof(bool), typeof(OdcExpander), new UIPropertyMetadata(false));



        /// <summary>
        /// Gets or sets the background color of the header in pressed mode.
        /// </summary>
        public Brush PressedHeaderBackground
        {
            get { return (Brush)GetValue(PressedHeaderBackgroundProperty); }
            set { SetValue(PressedHeaderBackgroundProperty, value); }
        }

        public static readonly DependencyProperty PressedHeaderBackgroundProperty =
            DependencyProperty.Register("PressedHeaderBackground", typeof(Brush), typeof(OdcExpander), 
            new UIPropertyMetadata(null, PressedHeaderBackgroundPropertyChangedCallback));


        public static void PressedHeaderBackgroundPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OdcExpander expander = (OdcExpander)d;
            expander.HasPressedBackground = e.NewValue != null;
        }

        public Thickness HeaderBorderThickness
        {
            get { return (Thickness)GetValue(HeaderBorderThicknessProperty); }
            set { SetValue(HeaderBorderThicknessProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderBorderThickness.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderBorderThicknessProperty =
            DependencyProperty.Register("HeaderBorderThickness", typeof(Thickness), typeof(OdcExpander), new UIPropertyMetadata(null));




        /// <summary>
        /// Gets or sets the foreground color of the header on mouse over.
        /// </summary>
        public Brush MouseOverHeaderForeground
        {
            get { return (Brush)GetValue(MouseOverHeaderForegroundProperty); }
            set { SetValue(MouseOverHeaderForegroundProperty, value); }
        }

        public static readonly DependencyProperty MouseOverHeaderForegroundProperty =
            DependencyProperty.Register("MouseOverHeaderForeground", typeof(Brush), typeof(OdcExpander), new UIPropertyMetadata(null));



        /// <summary>
        /// Specifies whether to show a elipse with the expanded/collapsed image.
        /// </summary>
        public bool ShowEllipse
        {
            get { return (bool)GetValue(ShowEllipseProperty); }
            set { SetValue(ShowEllipseProperty, value); }
        }

        public static readonly DependencyProperty ShowEllipseProperty =
            DependencyProperty.Register("ShowEllipse", typeof(bool), typeof(OdcExpander), new UIPropertyMetadata(false));


    }
}

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 MIT License


Written By
Software Developer (Senior)
Germany Germany
MCPD
Enterprise Application Developer 3.5
Windows Developer 3.5
.ASP.NET Developer 3.5
.NET 2.0 Windows Developer
.NET 2.0 Web Developer
.NET 2.0 Enterprise Application Developer


MCTS
.NET 3.5 Windows Forms Applications
.NET 3.5 ASP.NET Applications
.NET 3.5, ADO.NET Application Development
.NET 3.5 WCF
.NET 3.5 WPF
.NET 3.5 WF
Microsoft SQL Server 2008, Database Development
.NET 2.0 Windows Applications
.NET 2.0 Web Applications
.NET 2.0 Distributed Applications
SQL Server 2005
Sharepoint Services 3.0 Application Development
Windows Vista Client Configuration

Comments and Discussions