通过


BindableAttribute 类

定义

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

public ref class BindableAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class BindableAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All)>]
type BindableAttribute = class
    inherit Attribute
Public NotInheritable Class BindableAttribute
Inherits Attribute
继承
BindableAttribute
属性

示例

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

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.
        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。 首先,代码获取 PropertyDescriptorCollection 对象的所有属性。 接下来,代码索引到 PropertyDescriptorCollection 要获取 MyProperty的 。 最后,代码返回此属性的属性,并将其保存在属性变量中。 代码示例提供了两种不同的方法来检查值 BindableAttribute。 在第二个代码片段中,该示例调用该方法 Equals 。 在最后一个代码片段中,该示例使用 Bindable 属性检查值。

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

如果使用 > 标记类,请使用以下代码示例检查值。

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

注解

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

如果属性已标记为 BindableAttributetrue设置为”,则应为该属性引发属性更改通知。 这意味着,如果 Bindable 属性是 Yes,则支持双向数据绑定。 No如果是Bindable,仍可绑定到该属性,但不应显示在要绑定到的默认属性集中,因为它可能或可能不会引发属性更改通知。

注释

标记设置为 BindableAttribute 某个 true属性时,此属性的值将设置为常量成员 Yes。 对于标记为 BindableAttribute 设置为 false的属性,值为 No。 因此,若要在代码中检查此属性的值,必须将属性指定为BindableAttribute.Yes或指定。BindableAttribute.No

注意

只能在设计时使用此属性。 在运行时,任何内容都无法绑定到任何属性。

有关更多信息,请参阅特性

构造函数

名称 说明
BindableAttribute(BindableSupport, BindingDirection)

初始化 BindableAttribute 类的新实例。

BindableAttribute(BindableSupport)

使用值之一BindableSupport初始化类的新实例BindableAttribute

BindableAttribute(Boolean, BindingDirection)

初始化 BindableAttribute 类的新实例。

BindableAttribute(Boolean)

使用布尔值初始化类的新实例 BindableAttribute

字段

名称 说明
Default

指定 BindableAttribute默认值,即 No。 此字段是只读的。

No

指定属性通常不用于绑定。 此字段是只读的。

Yes

指定属性通常用于绑定。 此字段是只读的。

属性

名称 说明
Bindable

获取一个值,该值指示属性通常用于绑定。

Direction

获取一个值,该值指示此属性的数据绑定的方向或方向。

TypeId

在派生类中实现时,获取此 Attribute的唯一标识符。

(继承自 Attribute)

方法

名称 说明
Equals(Object)

确定两个 BindableAttribute 对象是否相等。

GetHashCode()

用作类的 BindableAttribute 哈希函数。

GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

确定此属性是否为默认值。

Match(Object)

在派生类中重写时,返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

显式接口实现

名称 说明
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,该信息可用于获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅