views:

525

answers:

2

I've read in a blog the following sentence:

The first rule of WPF:
Avoid control inheritance.

I've seen similar things in other places as well. However, I fail to understand the logic.

Moreover, I see suggestions here in StackOverflow that involves inheriting WPF controls (see the replies to my previous question for example).

I would like to understand why (and if) control inheritance should be avoided.

+5  A: 

WPF controls are "lookless". In other words, their look is separated from their behavior. More often than not, you actually want to customize the look - not the behavior. Unlike the world of Winforms, this does not require you to inherit a new control and override rendering logic. Instead, you set some properties on the control, resorting to overriding the template itself if you can't get the look you want from other properties.

Note that "avoid" means just that. Avoid inheritance if you can. In cases where you need to modify behavior, inheritance may well be the best option.

HTH, Kent

Kent Boogaart
+2  A: 

The blog from the link also states in the end that:

"With Styles, Templates, Decorators and attached properties and behaviors you can accomplish most of the things that you used to have to roll a new control for."

That is true. However, were it always true, the wpftoolkit team wouldn't have made their DataGrid as a subclass of MultiSelector.

Judge each case separately, I would say. Also check this answer.

kek444