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.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes

''' <summary> 
''' This is a ValidatingDP class that has a CurrentReading DP 
''' </summary> 
Public Class Gauge
    Inherits Control
    Public Sub New()
        MyBase.New()
    End Sub

#Region "CurrentReading DP"
    ''' <summary> 
    ''' CurrentReading 
    ''' </summary> 
    Public Shared ReadOnly CurrentReadingProperty As DependencyProperty = DependencyProperty.Register("CurrentReading", GetType(Double), GetType(Gauge), New FrameworkPropertyMetadata([Double].NaN, FrameworkPropertyMetadataOptions.None, New PropertyChangedCallback(AddressOf OnCurrentReadingChanged), New CoerceValueCallback(AddressOf CoerceCurrentReading)), New ValidateValueCallback(AddressOf IsValidReading))

    'property accessors 
    Public Property CurrentReading() As Double
        Get
            Return CDbl(GetValue(CurrentReadingProperty))
        End Get
        Set(ByVal value As Double)
            SetValue(CurrentReadingProperty, value)
        End Set
    End Property
#End Region

#Region "MinReading DP"
    ''' <summary> 
    ''' MinReading DP 
    ''' </summary> 
    Public Shared ReadOnly MinReadingProperty As DependencyProperty = DependencyProperty.Register("MinReading", GetType(Double), GetType(Gauge), New FrameworkPropertyMetadata(Double.NaN, FrameworkPropertyMetadataOptions.None, New PropertyChangedCallback(AddressOf OnMinReadingChanged), New CoerceValueCallback(AddressOf CoerceMinReading)), New ValidateValueCallback(AddressOf IsValidReading))

    'property accessors 
    Public Property MinReading() As Double
        Get
            Return CDbl(GetValue(MinReadingProperty))
        End Get
        Set(ByVal value As Double)
            SetValue(MinReadingProperty, value)
        End Set
    End Property
#End Region

#Region "MaxReading DP"
    ''' <summary> 
    ''' MaxReading 
    ''' </summary> 
    Public Shared ReadOnly MaxReadingProperty As DependencyProperty = DependencyProperty.Register("MaxReading", GetType(Double), GetType(Gauge), New FrameworkPropertyMetadata(Double.NaN, FrameworkPropertyMetadataOptions.None, New PropertyChangedCallback(AddressOf OnMaxReadingChanged), New CoerceValueCallback(AddressOf CoerceMaxReading)), New ValidateValueCallback(AddressOf IsValidReading))

    'property accessors 
    Public Property MaxReading() As Double
        Get
            Return CDbl(GetValue(MaxReadingProperty))
        End Get
        Set(ByVal value As Double)
            SetValue(MaxReadingProperty, value)
        End Set
    End Property
#End Region

#Region "CurrentReading Callbacks/validation/Coerces"
    ''' <summary> 
    ''' Coerce CurrentReading value if not within limits 
    ''' </summary> 
    Private Shared Function CoerceCurrentReading(ByVal d As DependencyObject, ByVal value As Object) As Object
        Dim g As Gauge = DirectCast(d, Gauge)
        Dim current As Double = CDbl(value)
        If current < g.MinReading Then
            current = g.MinReading
        End If
        If current > g.MaxReading Then
            current = g.MaxReading
        End If
        Return current
    End Function

    Private Shared Sub OnCurrentReadingChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        d.CoerceValue(MinReadingProperty)
        'invokes the CoerceValueCallback delegate ("CoerceMinReading") 
        d.CoerceValue(MaxReadingProperty)
        'invokes the CoerceValueCallback delegate ("CoerceMaxReading") 
    End Sub
#End Region

#Region "MinReading Callbacks/validation/Coerces"
    ''' <summary> 
    ''' Coerce MinReading value if not within limits 
    ''' </summary> 
    Private Shared Sub OnMinReadingChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        d.CoerceValue(MaxReadingProperty)
        'invokes the CoerceValueCallback delegate ("CoerceMaxReading") 
        d.CoerceValue(CurrentReadingProperty)
        'invokes the CoerceValueCallback delegate ("CoerceCurrentReading") 
    End Sub

    Private Shared Function CoerceMinReading(ByVal d As DependencyObject, ByVal value As Object) As Object
        Dim g As Gauge = DirectCast(d, Gauge)
        Dim min As Double = CDbl(value)
        If min > g.MaxReading Then
            min = g.MaxReading
        End If
        Return min
    End Function
#End Region

#Region "MaxReading Callbacks/validation/Coerces"
    ''' <summary> 
    ''' Coerce MaxReading value if not within limits 
    ''' </summary> 
    Private Shared Function CoerceMaxReading(ByVal d As DependencyObject, ByVal value As Object) As Object
        Dim g As Gauge = DirectCast(d, Gauge)
        Dim max As Double = CDbl(value)
        If max < g.MinReading Then
            max = g.MinReading
        End If
        Return max
    End Function

    Private Shared Sub OnMaxReadingChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        d.CoerceValue(MinReadingProperty)
        'invokes the CoerceValueCallback delegate ("CoerceMinReading") 
        d.CoerceValue(CurrentReadingProperty)
        'invokes the CoerceValueCallback delegate ("CoerceCurrentReading") 
    End Sub
#End Region

#Region "ValidReading"
    ''' <summary> 
    ''' Validate reading 
    ''' </summary> 
    Public Shared Function IsValidReading(ByVal value As Object) As Boolean
        Dim v As Double = CDbl(value)
        Return (Not v.Equals([Double].NegativeInfinity) AndAlso Not v.Equals([Double].PositiveInfinity))
    End Function
#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
Web04 | 2.8.141223.1 | Last Updated 11 Mar 2008
Article Copyright 2008 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid