Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

The App Bar and Controls - Buttons and Styles

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/18/2011 9:37:32 PM
The Style property is defined by FrameworkElement so of course it’s inherited by Control and ButtonBase and Button. Here’s a program that defines a Style for Button in the Resources section of the page:
Example 1. Project: ButtonStyles File: MainPage.xaml (excerpt)
<phone:PhoneApplicationPage.Resources>
<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="Foreground" Value="SkyBlue" />
<Setter Property="FontSize" Value="36" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Margin" Value="12" />
</Style>
</phone:PhoneApplicationPage.Resources>

As usual, the Style has an x:Key attribute and a TargetType. Three Button controls are arranged in a StackPanel. Each has a reference to the Style resource:

Example 2. Project: ButtonStyles File: MainPage.xaml (excerpt)
<Grid x:Name=" ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<Button Content="Button No. 1"
Style="{StaticResource btnStyle}" />

<Button Content="Button No. 2"
Style="{StaticResource btnStyle}" />

<Button Content="Button No. 3"
Style="{StaticResource btnStyle}" />
</StackPanel>
</Grid>

Here’s what it looks like:



Now change one of those three Button objects to a ToggleButton:

<ToggleButton Content="Button No. 2"
Style="{StaticResource btnStyle}" />

This causes a runtime error because you’re attempting to set a ToggleButton from a Style whose TargetType is Button.

But if you look at the class hierarchy, you’ll see that both Button and ToggleButton derive from ButtonBase. Try setting that as the TargetType in the Style:

<Style x:Key="btnStyle" TargetType="ButtonBase">
<Setter Property="Foreground" Value="SkyBlue" />
<Setter Property="FontSize" Value="36" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Margin" Value="12" />
</Style>

Now it works again. You can even change the TargetType to Control, but that’s about as far back as you can go with the particular example. If you change the TargetType to FrameworkElement you’ll get a runtime error because FrameworkElement doesn’t have Foreground or FontSize properties.

As a general rule, it makes sense to set TargetType to be the most general class that has all the properties you’re defining in the Style. You can inherit from styles based on derived classes. For example, you can begin with a Style with a TargetType of ButtonBase and then have two derived styles for a TargetType of Button and a TargetType of ToggleButton.

Other -----------------
- The App Bar and Controls - Toggling a Stopwatch
- The App Bar and Controls - The Button Hierarchy
- The App Bar and Controls - Theme Styles and Precedence
- The App Bar and Controls - The Concept of Content
- The App Bar and Controls - The Basic Button
- The App Bar and Controls - RangeBase and Slider
- The App Bar and Controls - Elements and Controls
- The App Bar and Controls - Jot and the ApplicationBar
- The App Bar and Controls - Jot and Touch
- The App Bar and Controls - Jot and Application Settings
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server