WPF - 更新自定义控件中的值
WPF - Update Value in Custom Control
这是一个非常简单的 class 初始设置控件是否处于编辑模式
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
结束Class
在使用依赖项 属性 时,您需要使用 GetValue
和 SetValue
,所以像这样的东西应该可以工作:
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
属性。
这是一个非常简单的 class 初始设置控件是否处于编辑模式
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
结束Class
在使用依赖项 属性 时,您需要使用 GetValue
和 SetValue
,所以像这样的东西应该可以工作:
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
属性。