DCSIMG
WPF is O.O, Control Templates are not! - Essential WPF

WPF is O.O, Control Templates are not!

The best feature in WPF, which I very appreciate, is that WPF is a real O.O framework. Its services are exposed as a pile of polymorphic types, having common base classes and interface implementation. As every aspect in the real life (especially in software), not everything is perfect and so is WPF.

One of the things that I'm a little disappointed with is Control Templates.

The big Why

Control Template is a strong mechanism in WPF. With Control Templates, a control’s logic is totally separated from its view. A Control Template is actually the control's view-factory. It is responsible for defining the structure of the view. For example, it is possible to replace a radio-button control view but still keeping its logic, by just replacing its control-template with a new one.

The problem is "replace it with a new one", which means: You can't reuse a template, unless you are copying it!

So what's wrong with copying the template? The ultimate answer is Maintenance!

What a control template writer should do when he/she based its template on one of the WPFs themes, let say Luna, and then Microsoft find a bug in this theme?

Having the patch will not resolve the problem since the control template writer has copy it, remember?

The same thing will happen to other controls which are based on the same template, or others.

Having my point clear, I will leave this question open to the Microsoft WPF team. I hope that they will find a workaround for this issue in the next release.

Published Monday, May 14, 2007 11:52 PM by Tomer Shamam
תגים:, , , ,

Comments

# re: WPF is O.O, Templates are not!

Tuesday, May 15, 2007 8:35 PM by Tamir Khason

Maybe I misunderstand you, but it's completely wrong

You can put your templates in separate file and reuse it in other project (as well as it done with all default templates). Once you are creating new control template without explicitely overriding it'll use it's default (or your custom default) template. So, what are you talking about?

# re: WPF is O.O, Templates are not!

Tuesday, May 15, 2007 9:44 PM by Mike Hillberg
There's a difference between ControlTemplate and DataTemplate. ControlTemplate is used to give a look to a control, DataTemplate generally gives a look to a data item. In the case of ContentControl types, such as RadioButton or Button, both kinds of template come into play. The built-in ControlTemplate on the Button leaves a hole in it in which to put your content (by "content" I mean the value of the Button.Content property). And you can provide a DataTemplate (via the Button.ContentTemplate property) to specify how your content should look. So you can use a DataTemplate to customize the look of your button without replacing the whole ControlTemplate. Here's an example of that approach: http://blogs.msdn.com/mikehillberg/archive/2007/02/01/ParameterizedTemplates.aspx

# re: WPF is O.O, Templates are not!

Wednesday, May 16, 2007 10:13 AM by Tomer Shamam

Hi Tamir, thanks for your comment.

Let’s say that you've got two projects, A and B and you are using the Microsoft's Luna template. So you copy the Microsoft's template to a separate file, as you said. In project ‘A’ you want a different combo box border, so you have to replace it with, let say an ellipse, and in project B you want a different content template, so you rewrite the template again, decorate it with a nice border. In both projects you have to rewrite the combo box template or part of it, so you have to copy-paste the original one (or part of it), and make few changes. Now Microsoft fixes a bug exactly in the part that you have copied. Now what should you do? It is simple, because you didn't "derive" the template (which you can't do in WPF), you need to fix it at least twice! Both in project A, B and also in the copy! That is the problem!

# re: WPF is O.O, Templates are not!

Wednesday, May 16, 2007 10:25 AM by Tomer Shamam

Hi Mike, thanks for your comment.

First I want to fix my words: I have written Data Template twice instead of Control Template (I have a lot of Data Template in mind in the last few days...:-)), sorry for the inconvenience (I have fixed it). Of course, Data Templates are for Data’s View and Control Templates are for Control’s View.

You have suggested a very nice solution. Unfortunately, it didn't solve the control-template, copy-paste issue :-)

At least you have understood my point.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems