Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 11 Mar 2008 CPOL
An introduction into WPF Dependancy Properties.
wpf_tour_beginners_part_4_dps.zip
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
Settings.settings
Callback_Validation_DPs
bin
Debug
Callback_Validation_DPs.exe
Callback_Validation_DPs.vshost.exe
Callback_Validation_DPs.vshost.exe.manifest
obj
Debug
TempPE
Properties
Settings.settings
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
Settings.settings
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
Settings.settings
wpf_tour_beginners_part_4_dps_vb.zip
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
Settings.settings
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
Settings.settings
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
Settings.settings
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
Settings.settings
obj
Debug
TempPE
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Shapes
Imports System.Windows.Media
Imports System.Windows.Media.Imaging

    ''' <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 Shared ReadOnly IsMasterHeaderAppliedProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsMasterHeaderApplied", GetType(Boolean), GetType(AttachedPropertyChildAdder), New FrameworkPropertyMetadata((AddressOf IsMasterHeaderAppliedChanged)))


    Public Shared Sub SetIsMasterHeaderApplied(ByVal element As DependencyObject, ByVal value As Boolean)
        element.SetValue(IsMasterHeaderAppliedProperty, value)
    End Sub
    Public Shared Function GetIsMasterHeaderApplied(ByVal element As DependencyObject) As Boolean
        Return CBool(element.GetValue(IsMasterHeaderAppliedProperty))
    End Function
#End Region

#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 Shared Sub IsMasterHeaderAppliedChanged(ByVal obj As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
            If CBool(args.NewValue) Then
                If TypeOf obj Is Window Then
                    Dim wnd As Window = DirectCast(obj, Window)
                    AddHandler wnd.Loaded, AddressOf wnd_Loaded
                End If
            End If
        End Sub

        ''' <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 Shared Sub wnd_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Try
                Dim dp As New DockPanel()
                dp.LastChildFill = True
                Dim sp As New StackPanel()
                dp.Children.Add(sp)
                sp.Background = New SolidColorBrush(Colors.CornflowerBlue)
                sp.Orientation = Orientation.Vertical
                sp.SetValue(DockPanel.DockProperty, Dock.Top)
                Dim bitmap As New BitmapImage(New Uri("Images/Header.png", UriKind.Relative))
                Dim image As New Image()
                image.Source = bitmap
                sp.Children.Add(image)
                Dim el As UIElement = TryCast(TryCast(DirectCast(sender, DependencyObject), Window).Content, UIElement)
                el.SetValue(DockPanel.DockProperty, Dock.Bottom)
                TryCast(DirectCast(sender, DependencyObject), Window).Content = Nothing
                dp.Children.Add(el)
                TryCast(DirectCast(sender, DependencyObject), Window).Content = dp
            Catch ex As Exception
                System.Diagnostics.Debug.WriteLine(String.Format("Exception : {}", ex.Message))
            End Try
        End Sub
#End Region
    End Class

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

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