更新:2007 年 11 月
获取或设置一个用于描述控件背景的画笔。这是一个依赖项属性。
命名空间:
System.Windows.Controls 程序集:
PresentationFramework(在 PresentationFramework.dll 中)
<BindableAttribute(True)> _
Public Property Background As Brush
Dim instance As Control
Dim value As Brush
value = instance.Background
instance.Background = value
[BindableAttribute(true)]
public Brush Background { get; set; }
[BindableAttribute(true)]
public:
property Brush^ Background {
Brush^ get ();
void set (Brush^ value);
}
/** @property */
/** @attribute BindableAttribute(true) */
public Brush get_Background()
/** @property */
/** @attribute BindableAttribute(true) */
public void set_Background(Brush value)
public function get Background () : Brush
public function set Background (value : Brush)
Background 属性仅适用于控件的休眠状态。控件的默认样式指定当控件的状态发生更改时的外观。例如,如果在 Button 上设置 Background 属性,则此按钮仅在未被按下或禁用时具有该值。如果要创建具有更高级的背景自定义项的控件,必须定义控件的样式。
此属性仅影响其模板使用 Background 属性作为参数的控件。对于其他控件,此属性不会产生任何影响。
下面的示例演示如何设置控件的背景属性。
<Button Name="btn" Background="Red"
Click="ChangeBackground">
Background
</Button>
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
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";
}
}
下面的示例演示一个模板,该模板会在某个按钮被按下时启用触发器以更改该按钮的背景。
<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>
Windows Vista
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0
参考
其他资源