Template with less hardcoding button contentok

Info icon This preview shows pages 787–790. Sign up to view the full content.

Example 20-21. Template with less hardcoding <Button Content="OK" Background="LightBlue"> <Button.Template> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="10"> <ContentPresenter Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding HorizontalContentAlignment}" /> Control Templates | 763
Image of page 787

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

</Border> </ControlTemplate> </Button.Template> </Button> This template is now looking like a candidate for reuse—we might want to apply this to lots of different buttons. The usual way to do this is to wrap it in a style. Styles A style is an object that defines a set of property values for a particular type of element. Since elements’ appearances are defined entirely by their properties— Template is a property, remember—this means a style can define as much of a control’s appearance as you like. It could be as simple as just setting some basic properties such as FontFamily and Background , or it could go as far as defining a template along with prop- erty values for every property that affects appearance. Example 20-22 sits between these two extremes—it puts the template from Example 20-21 into a style, along with settings for a few other properties. Example 20-22. Button style <UserControl.Resources> <Style x:Key="buttonStyle" TargetType="Button"> <Setter Property="Background" Value="LightBlue" /> <Setter Property="BorderBrush" Value="DarkBlue" /> <Setter Property="BorderThickness" Value="3" /> <Setter Property="FontSize" Value="20" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="10"> <ContentPresenter Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding HorizontalContentAlignment}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> 764 | Chapter 20: WPF and Silverlight
Image of page 788
</Style> </UserControl.Resources> Notice that the style is inside a Resources section—remember that all elements have a Resources property, which is a dictionary that can hold useful objects such as styles. We can then apply the style to an element like so: <Button Content="OK" Style="{StaticResource buttonStyle}" /> This will pick up all the properties from the style. Again notice the use of braces in the attribute value—this signifies that we’re using a markup extension , which is a type that works out at runtime how to set the property’s real value. We already saw the TemplateBinding markup extension, and now we’re using StaticResource , which looks up an entry in a resource dictionary.
Image of page 789

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 790
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern