WPF Custom Control VB.net

362 views Asked by At

I've created a custom control called ecTextBox in VB.NET which uses a control template in Generic.xaml. That works.

In code behind of the custom control I override metadata in the constructor:

Public Sub New()
    DefaultStyleKeyProperty.OverrideMetadata(GetType(ecTextBox), New FrameworkPropertyMetadata(GetType(ecTextBox)))
End Sub

In MainWindow.xaml I use the custom control with a simple

<ec:ecTextBox/>

That works fine.

But if I throw a second control or change the propertys of the first ecTextBox in MainWindow.xaml, I get the message "PropertyMetaData is already registered for type ecTextBox".

In StackOverflow I've read, that C# programmers should use the static-Keyword for the constructor. But if I change the constructor to

Shared Sub New
    DefaultStyleKeyProperty.OverrideMetadata(GetType(ecTextBox), New FrameworkPropertyMetadata(GetType(ecTextBox)))
End Sub

the second custom control don't uses the control template but appears as a normal TextBox without Border.

What is the correct way to override the metadata for all used ecTextBox controls and prevent the errors?

1

There are 1 answers

0
Michael Bayer On BEST ANSWER

This is the solution:

Public Sub New()
    MyBase.New()
End Sub

Shared Sub New()
    DefaultStyleKeyProperty.OverrideMetadata(GetType(ecTextBox), New FrameworkPropertyMetadata(GetType(ecTextBox)))
End Sub