共用方式為


Control.Background 屬性

定義

取得或設定一個筆刷,描述控制組的背景。

public:
 property System::Windows::Media::Brush ^ Background { System::Windows::Media::Brush ^ get(); void set(System::Windows::Media::Brush ^ value); };
[System.ComponentModel.Bindable(true)]
public System.Windows.Media.Brush Background { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.Background : System.Windows.Media.Brush with get, set
Public Property Background As Brush

屬性值

用來填滿控制背景的刷子。 預設值為 Transparent

屬性

範例

以下範例說明如何設定控制項的背景屬性。

<Button Name="btn" Background="Red" 
        Click="ChangeBackground">
  Background
</Button>
void ChangeBackground(object sender, RoutedEventArgs e)
{
    if (btn.Background == Brushes.Red)
    {
        btn.Background = new LinearGradientBrush(Colors.LightBlue, Colors.SlateBlue, 90);
        btn.Content = "Control background changes from red to a blue gradient.";
    }
    else
    {
        btn.Background = Brushes.Red;
        btn.Content = "Background";
    }
}
Private Sub ChangeBackground(ByVal Sender As Object, ByVal e As RoutedEventArgs)

    If (Equals(btn.Background, Brushes.Red)) Then

        btn.Background = New LinearGradientBrush(Colors.LightBlue, Colors.SlateBlue, 90)
        btn.Content = "Control background changes from red to a blue gradient."

    Else

        btn.Background = Brushes.Red
        btn.Content = "Background"

    End If
End Sub

以下範例展示了一個範本,允許觸發器在按下按鈕時改變背景。

<Button FontSize="16" FontWeight="Bold">Click the Background
  <Button.Template>
    <ControlTemplate TargetType="{x:Type Button}">
      <Border Background="{TemplateBinding Background}">
         <ContentPresenter/>
      </Border>
    </ControlTemplate>
  </Button.Template>

  <Button.Style>
    <Style TargetType="{x:Type Button}">
      <Setter Property="Background" Value="Blue"/>
      <Style.Triggers>
        <Trigger Property="IsPressed" Value="true">
          <Setter Property="Background" Value="Red"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

備註

Background 性質僅適用於控制組的靜態。 控制項的預設樣式會在控制狀態改變時指定其外觀。 例如,如果你在 上Button設定Background屬性,該按鈕只有在未被按下或停用時才會有該值。 如果你想建立一個能更進階自訂背景的控制項,你必須定義控制項的風格。

此性質僅影響模板以該 Background 屬性作為參數的控制項。 在其他控制項中,此性質不影響。

相依財產資訊

項目 價值
識別碼欄位 BackgroundProperty
元資料屬性設為 true AffectsRenderSubPropertiesDoNotAffectRender

適用於