Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Consistent Window Look & Feel

, 17 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
As part of a beginners WPF series (Beginners WPF series) that I am writing at codeproject, I just start to write an article on DependancyProperties. As part of that, I started to write a bunch of demo projects to illustrate the joy of DependancyProperties to people. As a side effect, I had to think

As part of a beginners WPF series (Beginners WPF series) that I am writing at codeproject, I just start to write an article on DependancyProperties. As part of that, I started to write a bunch of demo projects to illustrate the joy of DependancyProperties to people. As a side effect, I had to think up good uses for the demos that were not to involved.

I started to write one for Attached DependancyProperties, and realised that it was actually a fairly good idea. The idea being that you may want all your applications windows to have the same look and feel. Maybe a top banner, some content, and bottom status bar. So I had a think about this, and decided this could be easily achieved through the use of a single Attached DependancyProperty.

Using my solution is really easy, if you want to use the “Common” look and feel just set a DP in the Window declaration within the XAML or code behind, and that’s it. Shall we have a look at the code.

<Window x:Class=”Attached_Properties_DPs.Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
    xmlns:local=”clr-namespace:Attached_Properties_DPs”
    local:AttachedPropertyChildAdder.IsMasterHeaderApplied=”true”
    WindowStartupLocation=”CenterScreen”
    Title=”Attached_Properties_DPs” Height=”400″ Width=”600″>
        <!– Extra content will be added here at runtime if the
             local:AttachedPropertyChildAdder.
             IsMasterHeaderApplied=”true” is set to true
             try changing the value of this in the top of this
             file, set it false and run me.
             See that there is no header applied if its false,
             and there is if its true –>
        <Button x:Name=”btn1″ Content=”click me”
                Margin=”10,10,10,10″ Click=”btn1_Click”/>
</Window>

As you can see this example has the DP IsMasterHeaderApplied set to true, which means this Window will use the Common look and feel. So hows this work. Well its all down to the IsMasterHeaderApplied DP, which is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Imaging; 

namespace Attached_Properties_DPs
{
    /// <span class="code-SummaryComment"><summary></span>
    /// A simply show case, to demonstrate a usage of an attached
    /// DP property.
    /// This example lets Windows add a header portion to the their
    /// default content with some new Contents. Kind of like using
    /// Master Pages in ASP .NET
    /// <span class="code-SummaryComment"></summary></span>
    public class AttachedPropertyChildAdder
    {
        #region Register IsMasterHeaderApplied DP
        public static readonly DependencyProperty
            IsMasterHeaderAppliedProperty =
            DependencyProperty.RegisterAttached(
                “IsMasterHeaderApplied”,
                typeof(Boolean),
                typeof(AttachedPropertyChildAdder),
                new FrameworkPropertyMetadata(
                    IsMasterHeaderAppliedChanged)); 

        public static void SetIsMasterHeaderApplied(
            DependencyObject element, Boolean value)
        {
            element.SetValue(IsMasterHeaderAppliedProperty, value);
        }
        public static Boolean GetIsMasterHeaderApplied(
            DependencyObject element)
        {
            return (Boolean)element.GetValue(
                IsMasterHeaderAppliedProperty);
        }
        #endregion 

        #region PropertyChanged callback 

        /// <span class="code-SummaryComment"><summary></span>
        /// Is called whenever a user of the
        /// IsMasterHeaderApplied Attached DP changes
        /// the IsMasterHeaderApplied DP value
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name=”obj”></param></span>
        /// <span class="code-SummaryComment"><param name=”args”></param></span>
        public static void IsMasterHeaderAppliedChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs args)
        {
            if ((bool)args.NewValue)
            {
                if (obj is Window)
                {
                    Window wnd = (Window)obj;
                    wnd.Loaded += new RoutedEventHandler(wnd_Loaded);
                }
            }
        } 

        /// <span class="code-SummaryComment"><summary></span>
        /// Hook into the Window load event to replace the Content of the Window
        /// with some custom Content, to show case exactly how cool DPs are.
        ///
        /// In this example we are going to create a header for the Window.
        ///
        /// So setting the IsMasterHeaderApplied will make sure the Window
        /// gets a header applied.
        ///
        /// Kind of like Master Pages in ASP .NET
        /// <span class="code-SummaryComment"></summary></span>
        public static void wnd_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                DockPanel dp = new DockPanel();
                dp.LastChildFill = true;
                StackPanel sp = new StackPanel();
                dp.Children.Add(sp);
                sp.Background = new SolidColorBrush(Colors.CornflowerBlue);
                sp.Orientation = Orientation.Vertical;
                sp.SetValue(DockPanel.DockProperty, Dock.Top);
                BitmapImage bitmap = new BitmapImage(
                    new Uri(“Images/Header.png”, UriKind.Relative));
                Image image = new Image();
                image.Source = bitmap;
                sp.Children.Add(image);
                UIElement el =
                    ((DependencyObject)sender as Window).Content as UIElement;
                el.SetValue(DockPanel.DockProperty, Dock.Bottom);
                ((DependencyObject)sender as Window).Content = null;
                dp.Children.Add(el);
               ((DependencyObject)sender as Window).Content = dp;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(
                    string.Format(“Exception : {0}”,ex.Message));
            }
        }
        #endregion
    }
}

As can be seen, we simply use the Window.Loaded event, and keep the original Window content safe, and then create the “Common” layout, and then re-add the original content.

This maintains all the wiring of RoutedEvents that the original content had in place.

This example simply shows an image banner at the top of any Window that sets the IsMasterHeaderApplied DP to true. And when run it looks like this.

logo-thumb.png

Have a look at the original XAML, you see it declares the Button, but doesn’t declare the top banner, that is added by the IsMasterHeaderApplied DP.

Ok this is a very simple example, but lets say the “Common” content contained all the menus wired up to RoutedCommands and all the footer/status area etc etc.

I think it would work well.

License

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

Share

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
 
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
 
Both of these at Sussex University UK.
 
Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 17 Jun 2009
Article Copyright 2009 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid