Control.Background Control.Background Control.Background Control.Background Property

定義

取得或設定描述控制項背景的筆刷。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.

範例

下列範例示範如何設定控制項的背景屬性。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

適用於