Search code examples
wpfsetterattached-properties

WPF Attached Property in Style has no accessible Setter in VB.Net


I have create a Dependency Object Class:

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

End Class

My Style contains a Setter:

    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" />

Compilation gives me an error: XDG0013: The property "MonitorText" does not have an accessible setter.

What am I doing wrong?


Solution

  • The Set* and Get* accessors should only set and get the value of the attached property:

    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