次の方法で共有


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.FontControl.BackColor などがありますが、これらのプロパティは親プロパティに自身の値を照会します。

通常、ビジュアル デザイナは AmbientValueAttribute 属性を使用して、プロパティに永続的に適用する値を決定します。この値は、通常、プロパティに別のソースから値を取得させる働きをする値です。たとえば、Color.EmptyBackColor プロパティのアンビエント値として機能します。フォーム上にコントロールがあり、そのコントロールの BackColor プロパティがフォームの BackColor プロパティとは異なる色に設定されている場合、コントロールの BackColorColor.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

スレッド セーフ

この型の public static (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 名前空間