views:

161

answers:

3

Hi,

I am new to WPF and I have a doubt in modifying a control. I came to know that one can modify a control by changing its Control Template. i.e.,

<Button>
  <Button.Template>
    <ControlTemplate>
      ...
    </ControlTemplate>
  </Button.Template>
</Button>

Also we can edit a control utilizing the Content Model. i.e.,

<Button>
  <Grid>
    <CheckBox>
    </CheckBox>
    <RadioButton>
    </RadioButton>
    ...
  </Grid>
</Button>

What is the difference between these two techniques. Which one is the correct way to customize a control in WPF. Please help me in understanding this better.

A: 

Template can be placed into a resource and re-used for another button.

Changing content directly is not re-usable unless you make a UserControl out of it.

Which one you use depends on a concrete task and your personal preferences.

Sergey Aldoukhov
The content of a control most certainly is re-usable, either through defining the element as a resource and using ContentControls, or assigning the content as a DataTemplate.
rmoore
I think if you use ContentControl or DataTemplate, it would be not the same case # 2 in the original question.
Sergey Aldoukhov
+3  A: 

The primary difference between these two things, is that the ControlTemplate defines the look of the control. It is not actually placing content inside of it. At some location inside the content control, there should be some form of ContentPresenter. The built in controls are capable of this because they are what is known as 'lookless controls', and any custom controls created should also be lookless. When a control is not templated in a lookless manner but instead has a static layout, then the confusion you have run into can occur.

As for the correct way to do things, it depends on what you are trying to achieve. If you are attempting to change the control, such as the look and feel or the behavior, then using a ControlTemplate, (or DataTemplate, depending on what you are templating), is definitely the way to go. A good example of this is the CheckBox, belive it or not, the CheckBox is actually a ToggleButton (more or less), that through templating displays the togleablity in a bullet. Here's another very good example of how you can use Templates to do some very nifty things.

ControlTemplates should also be applied through Styles, instead of directly set on an element.

If you aren't actually aiming to modify the behavior or look of the control then using the content model is the correct approach.

WPF does this better then Silverlight, though I don't know if there are improvements in SL3.

rmoore
+1  A: 

My rule of thumb is that if it's possible to get what I want without changing the control template then you shouldn't change the control template.

control templates are a way to change the look&feel of a control, for example making a round button of changing the check mark graphics of a checkbox.

adding controls inside a control is simpler, for example place an image inside a button.

Also remember, there is a lot of functionality in the control template, including padding, content alignment, disabled look and more, if you change the control template you will have to add support for all of those features.

Nir