ValidateValueCallback Delegate

Definition

表示作為回呼的方法,該方法可驗證相依性屬性的有效值。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 

Parameters

value
Object

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

Return Value

Boolean

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

Inheritance
ValidateValueCallback

Examples

下列範例包含此回呼的執行,以驗證自訂相依性屬性的值範圍。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

Remarks

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,否則會 falseThe 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. 如果您需要可根據特定實例來變更屬性值的回呼,您應該使用 CoerceValueCallback 的組合,並 PropertyChangedCallback 回呼套用至屬性的屬性中繼資料。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.

Extension Methods

GetMethodInfo(Delegate)

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

Applies to

See also