Export (0) Print
Expand All

BindableAttribute Class

Specifies whether a property is typically used for binding.

For a list of all members of this type, see BindableAttribute Members.

System.Object
   System.Attribute
      System.ComponentModel.BindableAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.All)>
NotInheritable Public Class BindableAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.All)]
public sealed class BindableAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::All)]
public __gc __sealed class BindableAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.All)
class BindableAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You can specify this attribute for multiple properties on a control.

Initializes a new instance of the BindableAttribute class with a Boolean value.

If a property has been marked with the BindableAttribute constructor of the value true, then a property change notification should be raised for that property. This means that if Bindable is Yes, then two-way data binding is supported. 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   When you mark a property the BindableAttribute constructor of the value true , the value of this attribute is set to the constant member Yes. For a property marked with the BindableAttribute constructor of the value false, the value is No. Therefore, 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 Overview and Extending Metadata Using Attributes.

Example

[Visual Basic, C#, C++] The following example marks a property as appropriate to bind data to.

[Visual Basic] 
<Bindable(True)> _
Public Property MyProperty() As Integer
    Get
        ' Insert code here.
        Return 0
    End Get
    Set
         ' Insert code here.
    End Set
End Property

[C#] 
[Bindable(true)]
 public int MyProperty {
    get {
       // Insert code here.
       return 0;
    }
    set {
       // Insert code here.
    }
 }

[C++] 
public:

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

    [System::ComponentModel::Bindable(true)]
    __property void set_MyProperty(int) {
        // Insert code here.
    }

[Visual Basic, C#, C++] The next example shows how to check the value of the BindableAttribute for MyProperty. First, the code gets a PropertyDescriptorCollection with all the properties for the object. Next, it indexes into the PropertyDescriptorCollection to get MyProperty. Finally, it returns the attributes for this property and saves them in the attributes variable. The example presents two different ways to check the value of the BindableAttribute. In the second code fragment, the example calls the Equals method. In the last code fragment, the example uses the Bindable property to check the value.

[Visual Basic] 
' 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


[C#] 
// 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.
     }


[C++] 
using namespace System::ComponentModel;

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

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

// This is another way to see whether the property is bindable.
BindableAttribute* myAttribute =
   static_cast<BindableAttribute*>(attributes->Item[__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.
}

[Visual Basic, C#, C++] If you marked a class with the BindableAttribute, use the following code to check the value.

[Visual Basic] 
Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(MyProperty)
If attributes(GetType(BindableAttribute)).Equals(BindableAttribute.Yes) Then
    ' Insert code here.
End If 

[C#] 
AttributeCollection attributes = 
    TypeDescriptor.GetAttributes(MyProperty);
 if(attributes[typeof(BindableAttribute)].Equals(BindableAttribute.Yes)) {
    // Insert code here.
 }

[C++] 
using namespace System::ComponentModel;

AttributeCollection* attributes = TypeDescriptor::GetAttributes(__box(MyProperty));

if(attributes->Item[__typeof(BindableAttribute)]->Equals(BindableAttribute::Yes)) {
    // Insert code here.
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

BindableAttribute Members | System.ComponentModel Namespace | Attribute | PropertyDescriptor | AttributeCollection | PropertyDescriptorCollection

Show:
© 2014 Microsoft