WPF样式的附加属性在VB.Net中没有可访问的Setter

时间:2019-03-18 13:59:52

标签: wpf setter attached-properties

我已经创建了一个依赖对象类:

Public Class TextMonitoring
Inherits DependencyObject

Public Shared ReadOnly MonitorTextProperty As DependencyProperty = DependencyProperty.RegisterAttached("MonitorText",
                                                               GetType(Boolean),
                                                               GetType(TextMonitoring),
                                                               New PropertyMetadata(False, New PropertyChangedCallback(AddressOf MonitorTextChanged)))

Public Shared Function GetMonitorTextProperty(sender As DependencyObject) As Boolean
    Return CType(sender, PasswordBox).GetValue(MonitorTextProperty)
End Function

Public Shared Sub SetMonitorTextProperty(sender As DependencyObject)
    CType(sender, PasswordBox).SetValue(MonitorTextProperty, True)
End Sub

Public Shared Function GetMonitorText(sender As DependencyObject) As Boolean
    Return CType(sender, PasswordBox).GetValue(MonitorTextProperty)
End Function

Public Shared Sub SetMonitorText(sender As DependencyObject)
    CType(sender, PasswordBox).SetValue(MonitorTextProperty, True)
End Sub

Public Shared Sub MonitorTextChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)

End Sub

结束班级

我的风格包含一个塞特犬:

    xmlns:local="clr-namespace:TestAttachedProperty">

<Style TargetType="{x:Type PasswordBox}">

    <Setter Property="FontSize" Value="24" />
    <Setter Property="Padding" Value="10" />
    <Setter Property="Margin" Value="0 5 0 5" />

    <Setter Property="local:TextMonitoring.MonitorText" Value="True" />

编译给我一个错误:XDG0013:属性“ MonitorText”没有可访问的设置器。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

Set *和Get *访问器仅应设置并获取附加属性的值:

Public Class TextMonitoring
    Inherits DependencyObject

    Public Shared ReadOnly MonitorTextProperty As DependencyProperty = DependencyProperty.RegisterAttached("MonitorText",
                                                                    GetType(Boolean),
                                                                    GetType(TextMonitoring),
                                                                    New FrameworkPropertyMetadata(False, New PropertyChangedCallback(AddressOf MonitorTextChanged)))
    Public Shared Sub SetMonitorText(ByVal element As DependencyObject, ByVal value As Boolean)
        element.SetValue(MonitorTextProperty, value)
    End Sub
    Public Shared Function GetMonitorText(ByVal element As DependencyObject) As Boolean
        Return CType(element.GetValue(MonitorTextProperty), Boolean)
    End Function

    Private Shared Sub MonitorTextChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)

    End Sub

End Class