Udostępnij za pośrednictwem


Style.BasedOn Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets a defined style that is the basis of the current style.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property BasedOn As Style
public Style BasedOn { get; set; }
<object BasedOn="styleReference" .../>

XAML Values

  • styleReference
    A reference to an existing style. Typically, this uses a StaticResource reference to another style defined in resource dictionaries.

Property Value

Type: System.Windows.Style
A defined style that is the basis of the current style. The default value is nulla null reference (Nothing in Visual Basic).

Remarks

Each style supports only one BasedOn style. The BasedOn style cannot be changed when a Style is sealed. The TargetType property of a BasedOn style must match or be derived from the TargetType of a base style.

A Style is sealed either when the Seal method is called on the Style or when the Style is applied to a FrameworkElement. When a Style is sealed, all the base styles it derives from up the inheritance chain are also sealed.

When a base style and the BasedOn style have the same property, the property value set in the BasedOn style is used.

A style cannot be based on itself, or on any style that derives from it down the inheritance chain.

Examples

The following example creates a Style named InheritedStyle that is based on a Style named BaseStyle. InheritedStyle inherits the Background value of Yellow from BaseStyle and adds a Foreground value of Blue.

<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="BaseStyle" TargetType="Button">
            <Setter Property="Background" Value="Yellow" />
        </Style>
        <!--Create a Style based on BaseStyle-->
        <Style x:Key="InheritedStyle" TargetType="Button" BasedOn="{StaticResource BaseStyle}">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </StackPanel.Resources>
    <!--A button with default style-->
    <Button Content="HelloWorld" />
    <!--A button with base style-->
    <Button Content="HelloWorld" Style="{StaticResource BaseStyle}" />
    <!--A button with a style that is inherited from the BaseStyle-->
    <Button Content="HelloWorld" Style="{StaticResource InheritedStyle}" />
</StackPanel>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.