Control.Background Proprietà

Definizione

Ottiene o imposta un pennello che descrive lo sfondo di un controllo.

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

Valore della proprietà

Il pennello utilizzato per riempire lo sfondo del controllo. Il valore predefinito è Transparent.

Attributi

Esempio

Nell'esempio seguente viene illustrato come impostare la proprietà di sfondo di un controllo.

<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

Nell'esempio seguente viene illustrato un modello che consente a un trigger di modificare lo sfondo di un pulsante quando viene premuto.

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

Commenti

La Background proprietà si applica solo allo stato di riposo di un controllo. Lo stile predefinito del controllo specifica l'aspetto quando lo stato del controllo cambia. Ad esempio, se si imposta la Background proprietà su un Button, il pulsante ha tale valore solo quando non viene premuto o disabilitato. Se si vuole creare un controllo con una personalizzazione più avanzata dello sfondo, è necessario definire lo stile del controllo.

Questa proprietà influisce solo su un controllo il cui modello usa la Background proprietà come parametro. In altri controlli questa proprietà non ha alcun impatto.

Informazioni proprietà di dipendenza

Campo Identificatore BackgroundProperty
Proprietà dei metadati impostate su true AffectsRender, SubPropertiesDoNotAffectRender

Si applica a