共用方式為


Style.BasedOn 屬性

定義

取得或設定一個定義的風格,作為當前風格的基礎。

public:
 property System::Windows::Style ^ BasedOn { System::Windows::Style ^ get(); void set(System::Windows::Style ^ value); };
public System.Windows.Style BasedOn { get; set; }
[System.Windows.Markup.Ambient]
public System.Windows.Style BasedOn { get; set; }
member this.BasedOn : System.Windows.Style with get, set
[<System.Windows.Markup.Ambient>]
member this.BasedOn : System.Windows.Style with get, set
Public Property BasedOn As Style

屬性值

一個明確的風格,作為現行風格的基礎。 預設值為 null

屬性

範例

WPF 中的樣式可以有多種方式被擴充或繼承。 風格可以透過此物業建立基於其他風格。 當你使用這個屬性時,新樣式會繼承原本樣式的值,這些值在新樣式中並未明確重新定義。 以下範例中,繼Style2承 的YellowControl.Background值,並加上 Control.ForegroundBlue值。


<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>

同樣地,樣式也可以基於現有 WPF 元素的樣式,例如以下範例中新樣式是基於元素 TextBlock 的樣式。

  <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>

備註

如果你建立一個帶有屬性 TargetType 的樣式,並基於另一個同樣定義該 TargetType 屬性的樣式,該衍生樣式的目標型別必須與基礎樣式相同或由該型式衍生而來。

為特定類型定義的樣式也可以基於其他樣式,如下述範例所示。

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

備註

每種樣式只支援一個 BasedOn 數值。

XAML 屬性使用方式

<object BasedOn="myStyle" .../>

XAML 值

我的風格 一種既有的風格。 通常你會使用 Markup Extensions 和 WPF XAML 來指代現有的樣式。

適用於