|
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.
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