AmbientValueAttribute 类

指定要传递给属性的值,以使该属性从其他源获取其值。这称为“环境”。无法继承此类。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class AmbientValueAttribute
    Inherits Attribute
用法
Dim instance As AmbientValueAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public sealed class AmbientValueAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class AmbientValueAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public final class AmbientValueAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public final class AmbientValueAttribute extends Attribute

备注

如果控件上的属性具有环境行为,则必须存在此特性。环境属性向其父级查询它们的值,例如 Control.Font 属性或 Control.BackColor 属性。

通常,可视化设计器使用 AmbientValueAttribute 属性来决定为属性永久保存的值。这通常是一个使属性从另一个源获取其值的值。周围值的一个示例是 Color.Empty 作为 BackColor 属性的周围值。如果您在窗体上具有一个控件,并且该控件的 BackColor 属性被设置为与该窗体的 BackColor 属性不同的颜色,则您可以通过将该控件的 BackColor 设置为 Color.Empty 来将该控件的 BackColor 属性重置为该窗体的颜色。

示例

下面的代码示例演示使用 AmbientValueAttribute 强制实施名为 AlertForeColor 的属性的环境行为。有关完整的代码列表,请参见 如何:应用 Windows 窗体控件中的属性

<AmbientValue(GetType(Color), "Empty"), _
Category("Appearance"), _
DefaultValue(GetType(Color), "White"), _
Description("The color used for painting alert text.")> _
Public Property AlertForeColor() As Color
    Get
        If Me.alertForeColorValue = Color.Empty AndAlso Not (Me.Parent Is Nothing) Then
            Return Parent.ForeColor
        End If

        Return Me.alertForeColorValue
    End Get

    Set(ByVal value As Color)
        Me.alertForeColorValue = value
    End Set
End Property

' This method is used by designers to enable resetting the
' property to its default value.
Public Sub ResetAlertForeColor()
    Me.AlertForeColor = AttributesDemoControl.defaultAlertForeColorValue
End Sub

' This method indicates to designers whether the property
' value is different from the ambient value, in which case
' the designer should persist the value.
Private Function ShouldSerializeAlertForeColor() As Boolean
    Return Me.alertForeColorValue <> AttributesDemoControl.ambientColorValue
End Function
[AmbientValue(typeof(Color), "Empty")]
[Category("Appearance")]
[DefaultValue(typeof(Color), "White")]
[Description("The color used for painting alert text.")]
public Color AlertForeColor
{
    get
    {
        if (this.alertForeColorValue == Color.Empty &&
            this.Parent != null)
        {
            return Parent.ForeColor;
        }

        return this.alertForeColorValue;
    }

    set
    {
        this.alertForeColorValue = value;
    }
}

// This method is used by designers to enable resetting the
// property to its default value.
public void ResetAlertForeColor()
{
    this.AlertForeColor = AttributesDemoControl.defaultAlertForeColorValue;
}

// This method indicates to designers whether the property
// value is different from the ambient value, in which case
// the designer should persist the value.
private bool ShouldSerializeAlertForeColor()
{
    return (this.alertForeColorValue != AttributesDemoControl.ambientColorValue);
}

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.AmbientValueAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

AmbientValueAttribute 成员
System.ComponentModel 命名空间