ValidateValueCallback ValidateValueCallback ValidateValueCallback ValidateValueCallback Delegate

定義

表示作為回呼的方法,該方法可驗證相依性屬性的有效值。Represents a method used as a callback that validates the effective value of a dependency property.

public delegate bool ValidateValueCallback(System::Object ^ value);
public delegate bool ValidateValueCallback(object value);
type ValidateValueCallback = delegate of obj -> bool
Public Delegate Function ValidateValueCallback(value As Object) As Boolean 

參數

value
Object Object Object Object

要驗證的值。The value to be validated.

傳回值

System.Boolean

如果值已驗證則為 true,如果提交的值無效則為 falsetrue if the value was validated; false if the submitted value was invalid.

繼承
ValidateValueCallbackValidateValueCallbackValidateValueCallbackValidateValueCallback

範例

下列範例包含此回呼的執行, 以驗證自訂相依性屬性的值範圍。The following example includes an implementation of this callback to validate a range of values for a custom dependency property. 在此情況下, 屬性值會預期列舉, 而驗證會確保提供的值會評估為該列舉的成員。In this case the property value expects an enumeration, and the validation assures that the provided value evaluates to a member of that enumeration.

private static bool ShirtValidateCallback(object value)
{
	ShirtTypes sh = (ShirtTypes) value;
	return (sh==ShirtTypes.None || sh == ShirtTypes.Bowling || sh == ShirtTypes.Dress || sh == ShirtTypes.Rugby || sh == ShirtTypes.Tee);

}
Private Shared Function ShirtValidateCallback(ByVal value As Object) As Boolean
	Dim sh As ShirtTypes = CType(value, ShirtTypes)
	Return (sh=ShirtTypes.None OrElse sh = ShirtTypes.Bowling OrElse sh = ShirtTypes.Dress OrElse sh = ShirtTypes.Rugby OrElse sh = ShirtTypes.Tee)

End Function

備註

特定的簽章和相關的方法 ( DependencyProperty.Register RegisterAttached例如和唯讀的對等) 會使用以這個委派為基礎的回呼。Callbacks based on this delegate are used by certain signatures of DependencyProperty.Register and related methods, such as RegisterAttached and read-only equivalents. 如果您想要在每次validateValueCallback設定其有效的屬性值時進行特定驗證, 您應該使用接受參數的簽章。You should use signatures that take the validateValueCallback parameter if you want specific validation of the property value each time that its effective is set. 接著, 您應該執行此回呼, 讓它執行建議值的實際驗證。You should then implement this callback such that it performs the actual validation of the proposed value. true如果提交給回呼的值有效false , 則回呼應會傳回, 否則會傳回。The callback should return true if the value submitted to the callback is valid, and false otherwise. false值會在嘗試執行無效屬性集之屬性系統的特定部分中產生例外狀況, 因此您的應用程式應該準備好處理這些例外狀況。A false value will produce an exception in the specific portion of the property system where the invalid property set was attempted, so your application should be prepared to handle these exceptions.

驗證回呼會儲存在相依性屬性識別碼上, 而不是相依性屬性中繼資料。Validation callbacks are stored on dependency property identifiers, rather than dependency property metadata. 您的驗證回呼無法存取已設定屬性之的特定實例DependencyObject , 而且只會影響一般屬性所接受的值。Your validation callback does not have access to a specific instance of a DependencyObject on which the property is set, and can only influence what values are accepted for the property in general. 如果您需要可以根據特定實例來變更屬性值的回呼, 您應該使用CoerceValueCallbackPropertyChangedCallback回呼的組合, 以套用至屬性的屬性中繼資料。If you need a callback that can change property values based on a specific instance, you should use a combination of a CoerceValueCallback and PropertyChangedCallback callbacks as applied to the property metadata of your property. 您也可以將這些回呼套用至應該會影響屬性值的相關屬性。You might also apply these callbacks to related properties that should influence your property's value. 如需詳細資訊, 請參閱相依性屬性回呼和驗證For details, see Dependency Property Callbacks and Validation.

擴充方法

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。Gets an object that represents the method represented by the specified delegate.

適用於

另請參閱