C# Online.NET Visual C# Developer Center
Search

WPF Styles and Control Templates—Real Work

Visual C# Tutorials
.NET Framework Tutorials

WPF Styles and Control Templates

© 2005 O'Reilly Media

The Real Work

The last little bit of work is getting the padding right. Since the content presenter doesn’t have its own Padding property, we can’t bind the Padding property directly (it doesn’t have a Background property, either, which is why we used Rectangle and its Fill property). For properties that don’t have a match on the content presenter, you have to find mappings or compose the elements that provide the functionality that you’re looking for. For example, Padding is an amount of space inside of a control. Margin, on the other hand, is the amount of space around the outside of a control. Since they’re both of the same type, System.Windows.Thickness, if we could map the Padding from the inside of our button to the outside of the content control, our game would look very nice:

<Style TargetType="{x:Type Button}">
  <Setter Property="Background" Value="White" />
  <Setter Property="Padding" Value="10,5" />
  ...
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Button}">
        <Grid>
          <Rectangle 
            Fill="{TemplateBinding Property=Background}" />
          <ContentPresenter
            Content="{TemplateBinding Property=Content}"
            Margin="{TemplateBinding Property=Padding}" />
        </Grid>
    </ControlTemplate>
  </Setter.Value>
</Setter>
...
</Style>

Figure 5-20 shows our completed tic-tac-toe variation.


Image:ProgWPFfig5-20.jpg
Figure 5-20. Binding the Padding property to the Margin property


Like the mapping between Padding and Margin, building up the elements that give you the look you want and binding the appropriate properties from the templated parent is going to be a lot of the work of creating your own control templates.


Previous_Page_.gif Next_Page_.gif