Error: “ 'Subjects' property was already registered by 'Period' ” is raised when more than one control is placed on the form

主宰稳场 提交于 2019-12-13 04:30:20

问题


As you can see in the code below I have created a custom control named Period that inherits from Listbox. In it I have declared a read-only dependancy property named 'Subjects'. When a single Period is placed on the WPF window everything runs fine. However, when I place more than one I get the error mentioned in the title.

Here is the Period Class:

Public Class Period
Inherits System.Windows.Controls.ListBox

'-------- PROPERTIES --------'
Public ReadOnly Property Subjects() As ObservableCollection(Of Subject)
    Get
        Return Me.GetValue(SubjectsProperty)
    End Get
End Property

Private ReadOnly SubjectsPropertyKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("Subjects", GetType(ObservableCollection(Of Subject)), GetType(Period), New FrameworkPropertyMetadata(New ObservableCollection(Of Subject)))
Public ReadOnly SubjectsProperty As DependencyProperty = SubjectsPropertyKey.DependencyProperty

'-------- SUBROUTINES ---------'
Shared Sub New()
    'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
    'This style is defined in themes\generic.xaml
    DefaultStyleKeyProperty.OverrideMetadata(GetType(Period), New FrameworkPropertyMetadata(GetType(Period)))
End Sub

Public Sub New()
    MyBase.New()

    Me.SetValue(SubjectsPropertyKey, New ObservableCollection(Of Subject))


End Sub


'-------- METHODS ---------'
Public Sub AddSubject(ByRef subject As Subject)
    If Me.CheckForDuplicates(subject) = True Then
        MsgBox("This subject is already present in this period.")
    Else
        Dim SubjectsList As New ObservableCollection(Of Subject)
        SubjectsList = Me.GetValue(SubjectsProperty)
        SubjectsList.Add(subject)

        Me.SetValue(SubjectsPropertyKey, SubjectsList)
    End If
End Sub

Public Sub RemoveSubject(ByRef subject As Subject)
    If Me.CheckForDuplicates(subject) = False Then
        MsgBox("This subject is not present in this period.")
    Else
        Dim SubjectsList As New ObservableCollection(Of Subject)
        SubjectsList = Me.GetValue(SubjectsProperty)
        SubjectsList.Remove(subject)

        Me.SetValue(SubjectsPropertyKey, SubjectsList)
    End If
End Sub

Public Function CheckForDuplicates(ByRef subject As Subject) As Boolean
    Dim Conflict As Boolean

    If Subjects.Contains(subject) Then
        Conflict = True
    End If

    Return Conflict
End Function

Private Sub Period_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
    Me.ItemsSource = Subjects
End Sub
End Class

Here is the code for the window:

<Grid Background="#FF2B2B2B">
    <local:Period HorizontalAlignment="Left" VerticalAlignment="Top"/>
    <local:Period HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>

回答1:


SubjectsPropertyKey and SubjectsProperty has to be Shared. Otherwise it tries to register Subject with each instantiation of Period class and this fails after a successful registering.

See the Examples section in MSDN.



来源:https://stackoverflow.com/questions/18172527/error-subjects-property-was-already-registered-by-period-is-raised-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!