declouet
declouet

Reputation: 307

WPF - Should custom control attribute order matter?

If I write a custom WPF control, should I insure that the order in which the attributes are set in XAML do not affect the behavior of the control. For example:

<MyControl Prop1="Value" Prop2="Value" Prop3="Value" />

If the above XAML is instead written

<MyControl Prop3="Value" Prop2="Value" Prop1="Value" />

Should the user expect the state of the control to be the same in both cases? Or is it ok that I require the order to matter?

Upvotes: 1

Views: 446

Answers (3)

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

For standart XAML properties, the order can be arbitrary. But, if you create your own UserControl, the order can be important if the logic of your properties in PropertyMetadata has a dependency on some parameters-properties.

I think, the good practice to create a Control without specifying parameters in some order, because it's hard to remember all the order of properties, therefore it is not convenient for the end user.

For dependency properties, I use a default value if the user does not specify this value. Control should be designed so that some values ​​can not be specified at all or not in the correct order.

Upvotes: 0

Tony Vitabile
Tony Vitabile

Reputation: 8594

Order does not matter. The object is created & its properties set before any UI events occur. In fact, the XML specification says that attribute order is not defined & makes no difference.

Upvotes: 2

Michael Mairegger
Michael Mairegger

Reputation: 7301

You should not enforce the developer to follow such the attribute order. You cannot require that a developer remembers the right order to write the attributes.

I, eg., use an plugin that automatically orders the attributes.

Upvotes: 2

Related Questions