Прикрепленное свойство WPF в стиле не имеет доступного сеттера в VB.Net - PullRequest
2 голосов
/ 18 марта 2019

Я создал класс объекта зависимостей:

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 голосов
/ 18 марта 2019

Методы доступа 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
...