WPF - 自定义控件中的更新值

时间:2015-09-05 00:42:13

标签: wpf vb.net wpf-controls

这是一个非常简单的类,最初将控件设置为编辑模式或不是

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

在我想要更改IsNewRecord值之前哪个很好,当然它不起作用。我知道使用DependencyProperty是一种可行的方法,并尝试了一些变化,但仍然没有打开灯 - 这是一次尝试。指针将不胜感激。

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

结束班

1 个答案:

答案 0 :(得分:1)

在使用依赖项属性时,您需要使用GetValueSetValue,所以这样的事情应该有效:

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

删除Private _TextBoxNewRecord As Boolean变量,并在其余代码中使用IsNewRecord属性。