"Programming WPF" Translation 5th Chapter 4. Element type Style

Source: Internet
Author: User

The naming style is useful when you get a set of attributes and apply them to the elements of the feature. However, if you want to apply a uniform style to all instances that determine the element type, set targettype without a key, as shown in example 5-16.

Example 5-16

<!-- no Key -->
<Style TargetType="{x:Type Button}">
  <Setter Property="FontSize" Value="32" />
  <Setter Property="FontWeight" Value="Bold" />
</Style>
<!-- no Key -->
<Style TargetType="{x:Type TextBlock}">
  <Setter Property="FontSize" Value="32" />
  <Setter Property="FontWeight" Value="Thin" />
  <Setter Property="Foreground" Value="White" />
  <Setter Property="HorizontalAlignment" Value="Center" />
</Style>

<Button Grid.Row="0" Grid.Column="0" x:ID="cell00" />

<TextBlock Grid.Row="5" Grid.ColumnSpan="5" x:ID="statusTextBlock" />

As shown in example 5-16, we've got two styles, one with TargetType Button, no key, the other with TargetType TextBlock, no key. They all work the same way; When you create an instance of a button or TextBlock without realistically setting the Style property, it uses a style that matches the target type to the type of the control. Our element type style returns our game as shown in Figure 5-4.

Element type styles are convenient, and whenever you want to share an appearance for all instances of a particular element, it depends on the scope. So far, for example, in the top-level form, we've set a scope for the style in the example, example 5-17.

Example 5-17

<!-- Window1.xaml -->
<Window >
  <!-- every Button or TextBlock in the Window is affected -- >
  <Window.Resources>
    <Style TargetType="{x:Type Button}"></Style>
    <Style TargetType="{x:Type TextBlock}"></Style>
  </Window.Resources>

</Window>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.