Click here to Skip to main content
13,139,254 members (76,812 online)
Click here to Skip to main content

Stats

223.8K views
3.5K downloads
276 bookmarked
Posted 10 Feb 2008

WPF: A Beginner's Guide - Part 4 of n

, 11 Mar 2008
An introduction into WPF Dependancy Properties.
WPF_Tour_Beginners Part 4_DPs
Attached_Properties_DPs
bin
Debug
Attached_Properties_DPs.exe
Attached_Properties_DPs.vshost.exe
Attached_Properties_DPs.vshost.exe.manifest
Images
Header.png
obj
Debug
TempPE
Properties
Callback_Validation_DPs
bin
Debug
Callback_Validation_DPs.exe
Callback_Validation_DPs.vshost.exe
Callback_Validation_DPs.vshost.exe.manifest
obj
Debug
TempPE
Properties
DP_Custom_Inherited_Properties
bin
Debug
Custom_Inherited_DPs.exe
Custom_Inherited_DPs.vshost.exe
Custom_Inherited_DPs.vshost.exe.manifest
WpfApplication1.exe
WpfApplication1.vshost.exe.manifest
obj
Debug
TempPE
Properties
WpfApplication1.suo
Using_Inherited_Dps
bin
Debug
Using_Inherited_Dps.exe
Using_Inherited_Dps.vshost.exe
Using_Inherited_Dps.vshost.exe.manifest
obj
Debug
TempPE
Properties
WPF_Tour_Beginners_Part_4_DPs_VB
Attached_Properties_DPs
bin
Debug
Attached_Properties_DPs.exe
Attached_Properties_DPs.vshost.exe
Attached_Properties_DPs.vshost.exe.manifest
Images
Header.png
My Project
MyExtensions
obj
Debug
TempPE
Callback_Validation_DPs
bin
Debug
Callback_Validation_DPs.exe
Callback_Validation_DPs.vshost.exe
Callback_Validation_DPs.vshost.exe.manifest
My Project
MyExtensions
obj
Debug
TempPE
Custom_Inherited_DPs
bin
Debug
DP_Custom_Inherited_Properties.exe
DP_Custom_Inherited_Properties.vshost.exe
DP_Custom_Inherited_Properties.vshost.exe.manifest
My Project
MyExtensions
obj
Debug
TempPE
Using_Inherited_DPs
bin
Debug
Using_Inherited_Dps.exe
Using_Inherited_Dps.vshost.exe
Using_Inherited_Dps.vshost.exe.manifest
My Project
MyExtensions
obj
Debug
TempPE
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
{
    /// <summary>
    /// 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
    /// </summary>
    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

        /// <summary>
        /// Is called whenever a user of the IsMasterHeaderApplied Attached DP changes
        /// the IsMasterHeaderApplied DP value 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        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);
                }
            }
        }

        /// <summary>
        /// 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
        /// </summary>
        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 : {}",ex.Message));  
            }
        }
        #endregion
    }
}

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)

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 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • 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

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 11 Mar 2008
Article Copyright 2008 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid