BindableAttribute 类

指定成员是否通常用于绑定。无法继承此类。

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

语法

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

备注

您可以为控件上的多个成员(通常是属性 (Property))指定该属性 (Attribute)。

如果已将 BindableAttribute 设置为 true 来标记属性,则应引发该属性的属性更改通知。这意味着,如果 Yes 属性 (Property) 为 Bindable,则支持双向数据绑定。如果 BindableNo,则您仍可以绑定到该属性 (Property),但它不应该显示在默认的要绑定到的属性 (Property) 集中,因为它不一定引发属性 (Property) 更改通知。

提示

当使用设置为 trueBindableAttribute 标记某个属性 (Property) 时,此属性 (Attribute) 的值会被设置为常数成员 Yes。对于使用设置为 falseBindableAttribute 标记的属性 (Property),则此值为 No。因此,若要在代码中检查该属性 (Attribute) 的值,必须将该属性 (Attribute) 指定为 BindableAttribute.YesBindableAttribute.No

警告

您只能在设计时使用该属性 (Attribute)。您在运行时可以随意向任何属性 (Property) 进行绑定。

有关更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据

示例

下面的代码示例将属性标记为数据适于绑定到的属性。

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

   [System::ComponentModel::Bindable(true)]
   void set( int )
   {
      // Insert code here.
   }
}
/** @attribute Bindable(true)
 */
/** @property 
 */
public int get_MyProperty()
{
    // Insert code here.
    return 0;
} //get_MyProperty()

/** @property 
 */
public void set_MyProperty(int value)
{
    // Insert code here.
} //set_MyProperty

下一个代码示例演示如何检查 MyPropertyBindableAttribute 的值。首先,代码获取具有该对象的所有属性 (Property) 的 PropertyDescriptorCollection。接着,将代码编入 PropertyDescriptorCollection 的索引,以获取 MyProperty。最后,代码返回此属性 (Property) 的属性 (Attribute),并将它们保存到属性 (Attribute) 变量中。此代码示例介绍两种检查 BindableAttribute 值的不同方法。在第二个代码段中,该示例调用 Equals 方法。在最后一个代码段中,该示例使用 Bindable 属性 (Property) 检查该值。

    ' 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
   // 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.
}
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)
    .get_Item("MyProperty").get_Attributes();

// Checks to see if the value of the BindableAttribute is Yes.
if (attributes.get_Item(BindableAttribute.class.ToType())
    .Equals(BindableAttribute.Yes)) {
    // Insert code here.
}

// This is another way to see whether the property is bindable.
BindableAttribute myAttribute = (BindableAttribute)
    (attributes.get_Item(BindableAttribute.class.ToType()));
if (myAttribute.get_Bindable()) {
    // Insert code here.
}

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

如果将类标记为 BindableAttribute,则可使用下面的代码示例检查该值。

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

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.BindableAttribute

线程安全

此类型的任何公共静态(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

请参见

参考

BindableAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor
AttributeCollection 类
PropertyDescriptorCollection