.NET Framework Class Library
Style..::.BasedOn Property

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

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax

Visual Basic (Declaration)
Public Property BasedOn As Style
Visual Basic (Usage)
Dim instance As Style
Dim value As Style

value = instance.BasedOn

instance.BasedOn = value
C#
public Style BasedOn { get; set; }
Visual C++
public:
property Style^ BasedOn {
    Style^ get ();
    void set (Style^ value);
}
JScript
public function get BasedOn () : Style
public function set BasedOn (value : Style)
XAML Property Element Usage
<object>
  <object.BasedOn>
    <Style .../>
  </object.BasedOn>
</object>
XAML Attribute Usage
<object BasedOn="myStyle" .../>

XAML Values

myStyle

An existing style. Typically, you use the Markup Extensions and XAML to refer to an existing style.

Property Value

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

Each style only supports one BasedOn value.

Examples

There are several ways that styles in WPF can be extended or inherited. Styles can be based on other styles through this property. When you use this property, the new style will inherit the values of the original style that are not explicitly redefined in the new style. In the following example, Style2 inherits the Control..::.Background value of Yellow, and adds a Control..::.Foreground value of Blue.

XAML
<Style x:Key="Style1">
  <Setter Property="Control.Background" Value="Yellow"/>
</Style>

<Style x:Key="Style2" BasedOn="{StaticResource Style1}">
  <Setter Property="Control.Foreground" Value="Blue"/>
</Style>

Similarly, styles can be based on the style of an existing WPF element, as in the following example where the new style is based on the style of a TextBlock element.

XAML
  <Style
x:Key="TitleText"
BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="{x:Type TextBlock}">
    <Setter Property="FontSize" Value="32pt" />
    <Setter Property="Foreground">
      <Setter.Value>
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
          <LinearGradientBrush.GradientStops>
            <GradientStop Offset="0.0" Color="#90C117" />
            <GradientStop Offset="1.0" Color="#5C9417" />
          </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
      </Setter.Value>
    </Setter>
    <Setter Property="RenderTransform">
      <Setter.Value>
        <TranslateTransform X="0" Y="10"/>
      </Setter.Value>
    </Setter>
  </Style>
NoteNote:

If you create a style with a TargetType property and base it on another style that also defines a TargetType property, the target type of the derived style must be the same as or be derived from the type of the base style.

Styles defined for specific types can also be based on other styles, as in the following example.

XAML
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource Style1}">
  <Setter Property="Foreground" Value="Green"/>
</Style>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Tags :


Community Content

senyl
Potential gotcha
If you use BasedOn in the same file, the one you are deriving from has to be defined first.

So in this instance, you cannot define Style2 until you define Style1.
Tags :

Page view tracker