BindableAttribute Class

Definition

指定成员是否通常用于绑定。Specifies whether a member is typically used for binding. 无法继承此类。This class cannot be inherited.

public ref class BindableAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class BindableAttribute : Attribute
type BindableAttribute = class
    inherit Attribute
Public NotInheritable Class BindableAttribute
Inherits Attribute
Inheritance
BindableAttribute
Attributes

Examples

下面的代码示例根据适当的方式标记用于将数据绑定到的属性。The following code example marks a property as appropriate to bind data to.

property int MyProperty 
{
   [System::ComponentModel::Bindable(true)]
   int get()
   {
      // Insert code here.
      return 0;
   }

   [System::ComponentModel::Bindable(true)]
   void set( int )
   {
      // Insert code here.
   }
}
[Bindable(true)]
 public int MyProperty {
    get {
       // Insert code here.
       return 0;
    }
    set {
       // Insert code here.
    }
 }
<Bindable(True)> _
Public Property MyProperty() As Integer
    Get
        ' Insert code here.
        Return 0
    End Get
    Set
         ' Insert code here.
    End Set
End Property

下面的代码示例演示如何检查 MyPropertyBindableAttribute 的值。The next code example shows how to check the value of the BindableAttribute for MyProperty. 首先,该代码使用对象的所有属性获取 PropertyDescriptorCollectionFirst, the code gets a PropertyDescriptorCollection with all the properties for the object. 接下来,代码将索引到 PropertyDescriptorCollection 以获取 MyPropertyNext, the code indexes into the PropertyDescriptorCollection to get MyProperty. 最后,该代码将返回此属性的特性,并将它们保存在特性变量中。Finally, the code returns the attributes for this property and saves them in the attributes variable. 此代码示例提供了两种不同的方法来检查 BindableAttribute的值。The code example presents two different ways to check the value of the BindableAttribute. 在第二个代码片段中,该示例调用 Equals 方法。In the second code fragment, the example calls the Equals method. 在上一个代码段中,该示例使用 Bindable 属性来检查值。In the last code fragment, the example uses the Bindable property to check the value.

using namespace System::ComponentModel;

// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyProperty" ]->Attributes;

// Checks to see if the value of the BindableAttribute is Yes.
if ( attributes[ BindableAttribute::typeid ]->Equals( BindableAttribute::Yes ) )
{
   // Insert code here.
}

// This is another way to see whether the property is bindable.
BindableAttribute^ myAttribute = static_cast<BindableAttribute^>(attributes[ BindableAttribute::typeid ]);
if ( myAttribute->Bindable )
{
   // Insert code here.
}

// Yet another way to see whether the property is bindable.
if ( attributes->Contains( BindableAttribute::Yes ) )
{
   // Insert code here.
}
   // Gets the attributes for the property.
    AttributeCollection attributes =
       TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;

    // Checks to see if the value of the BindableAttribute is Yes.
    if(attributes[typeof(BindableAttribute)].Equals(BindableAttribute.Yes)) {
       // Insert code here.
    }

    // This is another way to see whether the property is bindable.
    BindableAttribute myAttribute =
       (BindableAttribute)attributes[typeof(BindableAttribute)];
    if(myAttribute.Bindable) {
       // Insert code here.
    }

// Yet another way to see whether the property is bindable.
if (attributes.Contains(BindableAttribute.Yes)) {
   // Insert code here.
}

    ' Gets the attributes for the property.
    Dim attributes As AttributeCollection = _
        TypeDescriptor.GetProperties(Me)("MyProperty").Attributes
    
    ' Checks to see if the value of the BindableAttribute is Yes.
    If attributes(GetType(BindableAttribute)).Equals(BindableAttribute.Yes) Then
        ' Insert code here.
    End If 
    
    ' This is another way to see whether the property is bindable.
    Dim myAttribute As BindableAttribute = _
        CType(attributes(GetType(BindableAttribute)), BindableAttribute)
    If myAttribute.Bindable Then
        ' Insert code here.
    End If 

' Yet another way to see whether the property is bindable.
If attributes.Contains(BindableAttribute.Yes) Then
' Insert code here.
End If

如果使用 BindableAttribute标记了类,请使用下面的代码示例检查值。If you marked a class with the BindableAttribute, use the following code example to check the value.

using namespace System::ComponentModel;
AttributeCollection^ attributes = TypeDescriptor::GetAttributes( MyProperty );
if ( attributes[ BindableAttribute::typeid ]->Equals( BindableAttribute::Yes ) )
{
   // Insert code here.
}
AttributeCollection attributes =
    TypeDescriptor.GetAttributes(MyProperty);
 if(attributes[typeof(BindableAttribute)].Equals(BindableAttribute.Yes)) {
    // Insert code here.
 }
Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(MyProperty)
If attributes(GetType(BindableAttribute)).Equals(BindableAttribute.Yes) Then
    ' Insert code here.
End If 

Remarks

您可以为控件的多个成员(通常为属性)指定此属性。You can specify this attribute for multiple members, typically properties, on a control.

如果某个属性已标记 BindableAttribute 设置为 true,则应为该属性引发属性更改通知。If a property has been marked with the BindableAttribute set to true, then a property change notification should be raised for that property. 这意味着,如果 YesBindable 属性,则支持双向数据绑定。This means that if the Bindable property is Yes, then two-way data binding is supported. 如果 NoBindable,则仍可以绑定到属性,但不应将其显示在要绑定到的默认属性集中,因为它可能会也可能不会引发属性更改通知。If Bindable is No, you can still bind to the property, but it should not be shown in the default set of properties to bind to, because it might or might not raise a property change notification.

Note

BindableAttribute 设置为 true的属性标记时,此属性的值将设置为常量成员 YesWhen you mark a property with BindableAttribute set to true, the value of this attribute is set to the constant member Yes. 对于标记为 BindableAttribute 设置为 false的属性,值为 NoFor a property marked with the BindableAttribute set to false, the value is No. 因此,若要在代码中检查此属性的值,必须将属性指定为 BindableAttribute.YesBindableAttribute.NoTherefore, to check the value of this attribute in your code, you must specify the attribute as BindableAttribute.Yes or BindableAttribute.No.

Caution

只能在设计时使用此属性。You can use this attribute at design time only. 在运行时不会阻止你绑定到任何属性。Nothing prevents you from binding to any property during run time.

有关更多信息,请参阅特性For more information, see Attributes.

Constructors

BindableAttribute(BindableSupport)

使用其中一个 BindableSupport 值初始化 BindableAttribute 类的新实例。Initializes a new instance of the BindableAttribute class with one of the BindableSupport values.

BindableAttribute(BindableSupport, BindingDirection)

初始化 BindableAttribute 类的新实例。Initializes a new instance of the BindableAttribute class.

BindableAttribute(Boolean)

使用布尔值初始化 BindableAttribute 类的新实例。Initializes a new instance of the BindableAttribute class with a Boolean value.

BindableAttribute(Boolean, BindingDirection)

初始化 BindableAttribute 类的新实例。Initializes a new instance of the BindableAttribute class.

Fields

Default

指定 BindableAttribute 的默认值,即 NoSpecifies the default value for the BindableAttribute, which is No. 此字段为只读。This field is read-only.

No

指定属性通常不用于绑定。Specifies that a property is not typically used for binding. 此字段为只读。This field is read-only.

Yes

指定属性通常用于绑定。Specifies that a property is typically used for binding. 此字段为只读。This field is read-only.

Properties

Bindable

获取指示某个属性通常用于绑定的值。Gets a value indicating that a property is typically used for binding.

Direction

获取指示此属性的数据绑定方向的值。Gets a value indicating the direction or directions of this property's data binding.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

Methods

Equals(Object)

确定两个 BindableAttribute 对象是否相等。Determines whether two BindableAttribute objects are equal.

GetHashCode()

用作 BindableAttribute 类的哈希函数。Serves as a hash function for the BindableAttribute class.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute()

确定此特性是否为默认特性。Determines if this attribute is the default.

Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Applies to

See also