IBindingList.Find(PropertyDescriptor, Object) 方法

定义

返回具有给定 PropertyDescriptor 的行的索引。Returns the index of the row that has the given PropertyDescriptor.

public:
 int Find(System::ComponentModel::PropertyDescriptor ^ property, System::Object ^ key);
public int Find (System.ComponentModel.PropertyDescriptor property, object key);
abstract member Find : System.ComponentModel.PropertyDescriptor * obj -> int
Public Function Find (property As PropertyDescriptor, key As Object) As Integer

参数

property
PropertyDescriptor

要对其进行搜索的 PropertyDescriptorThe PropertyDescriptor to search on.

key
Object

要搜索的 property 参数的值。The value of the property parameter to search for.

返回

Int32

具有给定 PropertyDescriptor 的行的索引。The index of the row that has the given PropertyDescriptor.

例外

示例

下面的代码示例演示如何实现 Find 方法。The following code example demonstrates how to implement the Find method.

    public class MyFontList : BindingList<Font>
    {

        protected override bool SupportsSearchingCore
        {
            get { return true; }
        }
        protected override int FindCore(PropertyDescriptor prop, object key)
        {
            // Ignore the prop value and search by family name.
            for (int i = 0; i < Count; ++i)
            {
                if (Items[i].FontFamily.Name.ToLower() == ((string)key).ToLower())
                    return i;
            }
            return -1;
        }
    }
}
Public Class MyFontList
    Inherits BindingList(Of Font)

    Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
        Get
            Return True
        End Get
    End Property
    
    Protected Overrides Function FindCore(ByVal prop As PropertyDescriptor, _
        ByVal key As Object) As Integer
        ' Ignore the prop value and search by family name.
        Dim i As Integer
        While i < Count
            If Items(i).FontFamily.Name.ToLower() = CStr(key).ToLower() Then
                Return i
            End If
            i += 1
        End While

        Return -1
    End Function
End Class

注解

此方法将选择参数的值 property 等于参数值的第一行 keyThis method will select the first row where the value of the property parameter equals the value of the key parameter.

如果为,则支持此方法 SupportsSearching true ; 否则,此方法将引发 NotSupportedExceptionThis method is supported if SupportsSearching is true, otherwise this method throws a NotSupportedException.

适用于