Control.Background 屬性

定義

取得或設定描述控制項背景的筆刷。Gets or sets a brush that describes the background of a control.

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; }
member this.Background : System.Windows.Media.Brush with get, set
Public Property Background As Brush

屬性值

用來填滿控制項背景的筆刷。The brush that is used to fill the background of the control. 預設為 TransparentThe default is Transparent.

屬性

範例

下列範例顯示如何設定控制項的 background 屬性。The following example shows how to set the background property of a control.

<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

下列範例顯示的範本可讓觸發程式在按下按鈕時變更其背景。The following example shows a template that enables a trigger to change the background of a button when it is pressed.

<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屬性只適用于控制項的停留狀態。The Background property applies only to the resting state of a control. 控制項的預設樣式會在控制項的狀態變更時指定其外觀。The default style of the control specifies its appearance when the state of the control changes. 例如, 如果您在Background Button上設定屬性, 只有在未按下或停用按鈕時, 才會有該值。For example, if you set the Background property on a Button, the button has that value only when it is not pressed or disabled. 如果您想要建立控制項, 使其具有更先進的背景自訂, 則必須定義控制項的樣式。If you want to create a control that has a more advanced customization of the background, you must define the control's style.

此屬性只會影響其範本使用Background屬性做為參數的控制項。This property only affects a control whose template uses the Background property as a parameter. 在其他控制項上, 此屬性不會有任何影響。On other controls, this property has no impact.

相依性屬性資訊Dependency Property Information

識別碼欄位Identifier field BackgroundProperty
中繼資料屬性設定為trueMetadata properties set to true AffectsRenderSubPropertiesDoNotAffectRenderAffectsRender, SubPropertiesDoNotAffectRender

適用於