Click here to Skip to main content
Click here to Skip to main content

You see, you don’t always need code behind.

, 8 Jun 2011
Rate this:
Please Sign up or sign in to vote.
In this post, I’m going to demonstrate a simple technique to add resize and close functionality to window buttons when you want to custom draw your window chrome without having to add code behind the window.

By now, you should be aware that I’m a big fan of attached behaviors. In this post, I’m going to demonstrate a simple technique to add resize and close functionality to window buttons when you want to custom draw your window chrome without having to add code behind the window. This is going to be a quick post, because it’s just so darned easy.

namespace AttachedTitleButtonsSample
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Interactivity;

    /// <span class="code-SummaryComment"><summary>
</span>    /// Attach this behaviour to a button to enable a button 
    /// to change the window state without
    /// having to write any code behind the view.
    /// <span class="code-SummaryComment"></summary>
</span>    public partial class TitleButtonBehavior : Behavior<Button>
    {
        /// <span class="code-SummaryComment"><summary>
</span>        /// The tile button action to apply.
        /// <span class="code-SummaryComment"></summary>
</span>        public enum TitleButtonAction
        {
            /// <span class="code-SummaryComment"><summary>
</span>            /// Close the application
            /// <span class="code-SummaryComment"></summary>
</span>            Close,
            /// <span class="code-SummaryComment"><summary>
</span>            /// Maximize the application
            /// <span class="code-SummaryComment"></summary>
</span>            Maximize,
            /// <span class="code-SummaryComment"><summary>
</span>            /// Minimize the application
            /// <span class="code-SummaryComment"></summary>
</span>            Minimize,
            /// <span class="code-SummaryComment"><summary>
</span>            /// Reset the application to normal
            /// <span class="code-SummaryComment"></summary>
</span>            Normal
        }

        /// <span class="code-SummaryComment"><summary>
</span>        /// Gets or sets the button behavior.
        /// <span class="code-SummaryComment"></summary>
</span>        public TitleButtonAction ButtonBehavior { get; set; }

        /// <span class="code-SummaryComment"><summary>
</span>        /// Add the click handler when this is attached.
        /// <span class="code-SummaryComment"></summary>
</span>        protected override void OnAttached()
        {
            this.AssociatedObject.Click += AssociatedObject_Click;
            base.OnAttached();
        }

        /// <span class="code-SummaryComment"><summary>
</span>        /// Remove the click handler when this is detached.
        /// <span class="code-SummaryComment"></summary>
</span>        protected override void OnDetaching()
        {
            this.AssociatedObject.Click += AssociatedObject_Click;
            base.OnDetaching();
        }

        /// <span class="code-SummaryComment"><summary>
</span>        /// Change the window state when the button is clicked.
        /// <span class="code-SummaryComment"></summary>
</span>        void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            switch (ButtonBehavior)
            {
                case TitleButtonAction.Close:
                    Application.Current.MainWindow.Close();
                    break;
                case TitleButtonAction.Maximize:
                    Application.Current.MainWindow.WindowState = WindowState.Maximized;
                    break;
                case TitleButtonAction.Minimize:
                    Application.Current.MainWindow.WindowState = WindowState.Minimized;
                    break;
                case TitleButtonAction.Normal:
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                    break;
            }
        }
    }
}

Basically, all you need to do is create an attached behavior that hooks up to the Click event of the button and sets the size based on the appropriate value from the enumeration.

Sample Application

I’ve attached a sample application that demonstrates this technique in action. As always, when you download the sample, you’ll need to rename it from a doc to a zip file.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pete O'Hanlon
CEO
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.
 
I am not the Stig, but I do wish I had Lotus Tuned Suspension.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralError PinmemberPritesh Aryan15-Jun-11 2:37 
GeneralRe: Error PinmvpPete O'Hanlon15-Jun-11 3:00 
Generaltypo in code? PinmemberMadJohn10-Jun-11 6:07 
GeneralRe: typo in code? PinmvpPete O'Hanlon10-Jun-11 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 8 Jun 2011
Article Copyright 2011 by Pete O'Hanlon
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid