"Programming WPF" translation 6th Chapter 2. Resources and Styles

Source: Internet
Author: User

The style mechanism of WPF has been used to locate styles in the resource system. As you can see in chapter 5th, the style is defined in the element's resource fragment, and the style is referenced by its name, as shown in example 6-18:

Example 6-18

<Window x:Class="ResourcePlay.Window1" Text="ResourcePlay"
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">

     <Window.Resources>
        <Style x:Key="myStyle">
            <Setter Property="Button.FontSize" Value="36" />
        </Style>
    </Window.Resources>

    <Grid>
        <Button Style="{StaticResource myStyle}">Hello</Button>
    </Grid>
</Window>

However, how to define a style, apply it automatically to an element without having to display the resource you specify to reference--this is achievable and useful--when you need to apply a style to all elements of a unique type, rather than adding a resource reference to each element. Example 6-19 makes some modifications to example 6-18, demonstrating the functionality of implicit declarations.

Example 6-19

<Window x:Class="ResourcePlay.Window1" Text="ResourcePlay"
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">

     <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Button.FontSize" Value="36" />
        </Style>
    </Window.Resources>

    <Grid>
        <Button>Hello</Button>
    </Grid>
</Window>

Note that the button label no longer has its particular style attribute. However, this style is still applied to the button by targettype instead of defining a key, which uses x:type to set the TargetType, and then notifies the XAML to provide a System.Type object for the TargetType class.

If FrameworkElement does not display the specified style, it always looks for a style resource that uses its own type as its target type.

If you set up some non-styled resources, such as SolidColorBrush, and you set the type of x:key as a UI element, an error occurs if you try to use the type of the element. This is because you created a style with TargetType but did not specify x:key,x:key implicitly set to the same as TargetType. This key is used to locate style. Therefore, in general, you should avoid setting the x:key to the type object.

Because the element will search for its style in the resource, you can take advantage of the system-level resources. You can define a style resource in a local scope, if you only want to affect a small number of elements, or in a broad scope, such as Window.resource, or in the scope of the application. And the style may be deferred to the system level. The connection between this style and resources is the key to using the skin and the body

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.