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 AffectsRender, SubPropertiesDoNotAffectRender

適用於