gchq
gchq

Reputation: 1775

WPF - Update Value in Custom Control

Here is a very simple class that initially sets a control as in edit mode or not

Public Class DateTBx
    Inherits DevComponents.WpfEditors.DateTimeInput
    Private _TextBoxNewRecord As Boolean

    Public Property IsNewRecord As Boolean
        Get
            Return _TextBoxNewRecord
        End Get
        Set(value As Boolean)
            _TextBoxNewRecord = value
        End Set
    End Property
    Protected Overrides Sub OnInitialized(e As System.EventArgs)
        MyBase.OnInitialized(e)
        VerticalAlignment = Windows.VerticalAlignment.Center
        HorizontalAlignment = Windows.HorizontalAlignment.Left
        BorderBrush = New SolidColorBrush(Colors.Silver)
        ClearButtonVisibility = Windows.Visibility.Hidden

        If _TextBoxNewRecord = True Then
            BorderThickness = New Thickness(1)
            IsEnabled = True
        Else
            BorderThickness = New Thickness(0)
            IsEnabled = False
        End If

    End Sub


End Class

Which is great until I want to change the IsNewRecord value, then of course it doesn't work. I know that using DependencyProperty is the way to go and have tried a few variations but still the light hasn't switched on - here is one attempt. A pointer would be appreciated.

Public Class DateTBx
Inherits DevComponents.WpfEditors.DateTimeInput
Public Shared IsNewRecordProperty As DependencyProperty = DependencyProperty.Register("IsNewRecord", GetType(Boolean), GetType(DateTBx), New PropertyMetadata(New PropertyChangedCallback(AddressOf IsNewRecordChanged)))
Private _TextBoxNewRecord As Boolean

Public Property IsNewRecord As Boolean
    Get
        Return _TextBoxNewRecord
    End Get
    Set(value As Boolean)
        _TextBoxNewRecord = value
    End Set
End Property



Protected Overrides Sub OnInitialized(e As System.EventArgs)
    MyBase.OnInitialized(e)
    VerticalAlignment = Windows.VerticalAlignment.Center
    HorizontalAlignment = Windows.HorizontalAlignment.Left
    BorderBrush = New SolidColorBrush(Colors.Silver)
    ClearButtonVisibility = Windows.Visibility.Hidden

    If _TextBoxNewRecord = True Then
        BorderThickness = New Thickness(1)
        IsReadOnly = False
    Else
        BorderThickness = New Thickness(0)
        IsReadOnly = True
    End If

End Sub

Private Shared Sub IsNewRecordChanged(Sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
    Dim vControl As DateTBx = TryCast(Sender, DateTBx)
    Dim vBoolean As Boolean = e.NewValue
    If vBoolean = True Then
        vControl.BorderThickness = New Thickness(1)
        vControl.IsReadOnly = False
    Else
        vControl.BorderThickness = New Thickness(0)
        vControl.IsReadOnly = True
    End If
End Sub

End Class

Upvotes: 0

Views: 61

Answers (1)

Mark
Mark

Reputation: 8160

You need to use GetValue and SetValue when working with a dependency property, so something like this should work:

Public Property IsNewRecord As Boolean
    Get
        Return CBool(GetValue(IsNewRecordProperty))
    End Get
    Set(value As Boolean)
        SetValue(IsNewRecordProperty, value)
    End Set
End Property

Remove the Private _TextBoxNewRecord As Boolean variable, and use the IsNewRecord property in the rest of the code.

Upvotes: 1

Related Questions