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