More on Triggers and Styles and Control Templates - The Real Work
(Page 5 of 5 )
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 usedRectangleand itsFillproperty). 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,Paddingis 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 thePaddingfrom 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.

Figure 5-20. Binding the Padding property to the Margin property (Color Plate 15)
Like the mapping betweenPaddingandMargin, 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.
Where Are We?
Styles enable you to define a policy for setting the dependency properties of visual elements. The sets of properties can be named and applied manually or programmatically by name, or applied automatically using element-typed styles. In addition to providing constant dependency-property values, styles can contain condition-based property values based on other dependency properties, data properties, or events. And, if setting properties isn’t enough to get the look you’re after, you can replace a lookless control’s entire rendering behavior using a control template.
But that’s not all there is to styles. For information about how animations work, you’ll want to read Chapter 8, and for information about styles as related to resources, themes, and skins, you’ll want to read Chapter 6.
* Moving groups of settings into styles also allows for easier skinning and theming, as described in Chapter 6.
* The null value is set via a XAML markup extension, which you can read more about in Appendix A.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter five of the book Programming Windows Presentation Foundation, written by Chris Sells and Ian Griffiths (O'Reilly; ISBN: 0596101139). Check it out today at your favorite bookstore. Buy this book now.
|
|