Share via

Duplicate Dependency Property Registration

RogerSchlueter-7899 1,731 Reputation points
2023-12-28T18:26:47.3533333+00:00

Here is my UserControl:

Public Class Categories
   Inherits UserControl
   ....
   Private ReadOnly AddSplitProperty As DependencyProperty = DependencyProperty.Register("AddSplit", GetType(Boolean), GetType(Categories))

   Public Property AddSplit As Boolean
      Get
         Return CBool(GetValue(AddSplitProperty))
      End Get
      Set(value As Boolean)
         SetValue(AddSplitProperty, value)
   End Property

End Class

The class Categories is used in a different Window as follows (PIM is the name of the application):

<Window

<Window
....
	xmlns:self="clr-namespace:PIM"
....
    <self:Categories AddSplit="True" />
....
</Window>

Running the application results in the following error:

System.ArgumentException
  HResult=0x80070057
  Message='AddSplit' property was already registered by 'Categories'.
  Source=WindowsBase

(The above is supposed to be a quote but it gets saved as code.)

Apparently, 'AddSplit' is trying to be registered twice. Why?

Developer technologies | VB
0 comments No comments

Answer accepted by question author

gekka 14,146 Reputation points MVP Volunteer Moderator
2023-12-29T08:19:10.0666667+00:00

Registering a DependencyProperty in instance field is trying to register it every time that instance is created.

It should be shared field.

Private Shared ReadOnly AddSplitProperty As DependencyProperty = DependencyProperty.Register("AddSplit", GetType(Boolean), GetType(Categories))

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2023-12-28T20:30:42.68+00:00

    Probably another property uses the same parameter “AddSplit” by mistake.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.