DependencyProperty.IsValidType(Object) 方法

定义

确定指定的值对于该依赖项对象的类型是否可接受(与原依赖项对象注册中提供的属性类型相对照)。Determines whether a specified value is acceptable for this dependency property's type, as checked against the property type provided in the original dependency property registration.

public:
 bool IsValidType(System::Object ^ value);
public bool IsValidType (object value);
member this.IsValidType : obj -> bool
Public Function IsValidType (value As Object) As Boolean

参数

value
Object

要检查的值。The value to check.

返回

如果指定的值是已注册的属性类型或可接受的派生类型,则为 true;否则为 falsetrue if the specified value is the registered property type or an acceptable derived type; otherwise, false.

示例

下面的示例在对依赖属性调用 SetValue 之前,使用 IsValidType 作为检查。The following example uses IsValidType as a check before calling SetValue on the dependency property.

void TrySetValue(DependencyObject target, DependencyProperty dp, object providedValue) {
  if (dp.IsValidType(providedValue))
  {
    target.SetValue(dp, providedValue);
  }
}
Private Sub TrySetValue(ByVal target As DependencyObject, ByVal dp As DependencyProperty, ByVal providedValue As Object)
  If dp.IsValidType(providedValue) Then
    target.SetValue(dp, providedValue)
  End If
End Sub

注解

null 的值是引用类型依赖属性或 Nullable<T> 依赖属性的有效类型,并将在这些情况下返回 trueA value of null is a valid type for reference type dependency properties, or for a Nullable<T> dependency property, and would return true for these cases. 如果依赖属性既不是引用,也不是 Nullable<T> 类型,则 IsValidType 将为 null 值返回 false,而不是引发异常。In cases where the dependency property is neither a reference nor a Nullable<T> type, IsValidType will return false for a null value rather than raise an exception.

适用于