WPF - The dependency property is already registered


This is the error you will face when you are copy pasting the code to write dependency property. JJoking…That is not the reason of this error. But I have received it when I copy pasted dependency property code from one of the site.
You will land up in this error when you have dependency property written in following way –
public readonly DependencyProperty SelectedItems2 =
            DependencyProperty.Register("SelectedItems2", typeof(s07ing), typeof(ListBox), new PropertyMetadata(null));

What is missing here? - STATIC keyword.
The problem lies in a way you register your dependency property. You should not register in the default cons07uctor of the class, but in static cons07uctor or initialize it at the time of declaration and preceding with Static keyword. Best way to write dependency property without error is code snippet. Type ‘propdp’ then press tab twice. And then customize the property as per your requirement. This is how you can avoid this error; avoid learning of dependency property syntax and save time.
public static readonly DependencyProperty SelectedItems2 =
            DependencyProperty.Register("SelectedItems2", typeof(s07ing), typeof(ListBox), new PropertyMetadata(null));

Hope this helps.
Cheers…
Happy Properting!!!

Comments

Popular posts from this blog

The request has both SAS authentication scheme and 'Bearer' authorization scheme. Only one scheme should be used

Getting Started with Logic Apps - AS2

How to Debug and Trace request in Azure APIM - Portal, Postman, RequestBin