0

我创建了一个TextBox继承标准 WPF 的 Extended ,TextBox我现在要做的是创建其他扩展控件类型,如 a TextBlockListBoxComboBox。所有控件都将具有相同的 DependencyProperties ,如下所示,所以我试图找到一种方法来实现这无需重复DependencyProperty每个新扩展控件背后的代码。

Public Class ExtendedTextBox
    Inherits TextBox

    Public Shared MandatoryProperty As DependencyProperty = DependencyProperty.Register("Mandatory", GetType(Boolean), GetType(ExtendedTextBox)) 

    Public Shared ReadOnly HasAnyErrorsProperty As DependencyProperty = DependencyProperty.Register("HasAnyErrors", GetType(Boolean), GetType(ExtendedTextBox))
End Class
4

1 回答 1

1

您可以定义可以在任何设置的附加属性UIElement

Public Class MyProperties
    Public Shared ReadOnly MandatoryProperty As DependencyProperty = DependencyProperty.RegisterAttached("Mandatory", GetType(Boolean), GetType(MyProperties))
    Public Shared Sub SetMandatory(ByVal element As UIElement, ByVal value As Boolean)
        element.SetValue(MandatoryProperty, value)
    End Sub
    Public Shared Function GetMandatory(ByVal element As UIElement) As Boolean
        Return CType(element.GetValue(MandatoryProperty), Boolean)
    End Function
End Class

XAML:

<TextBox local:MyProperties.Mandatory="True" />
<ListBox local:MyProperties.Mandatory="False" />
于 2018-09-20T13:58:18.467 回答