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.

傳回

Boolean

如果指定的值為註冊的屬性型別或可接受的衍生型別則為 true,否則為 falsetrue if the specified value is the registered property type or an acceptable derived type; otherwise, false.

範例

下列範例會在 IsValidType 呼叫相依性屬性之前,使用做為檢查 SetValueThe 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> 而且會在 true 這些情況下傳回。A 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 false null 值,而不是引發例外狀況。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.

適用於