CoerceValueCallback Delegate

Definition

提供方法的範本,該方法會在相依性屬性值重新評估時或特別要求強制型轉 (Coercion) 時被呼叫。Provides a template for a method that is called whenever a dependency property value is being re-evaluated, or coercion is specifically requested.

public delegate System::Object ^ CoerceValueCallback(DependencyObject ^ d, System::Object ^ baseValue);
public delegate object CoerceValueCallback(DependencyObject d, object baseValue);
type CoerceValueCallback = delegate of DependencyObject * obj -> obj
Public Delegate Function CoerceValueCallback(d As DependencyObject, baseValue As Object) As Object 

Parameters

d
DependencyObject

屬性所在的物件。The object that the property exists on. 當叫用 (Invoke) 回呼時 (Callback),屬性系統將傳遞這個值。When the callback is invoked, the property system will pass this value.

baseValue
Object

在嘗試任何強制型轉之前,屬性的新值。The new value of the property, prior to any coercion attempt.

Return Value

Object

強制型轉的值 (具有適當的型別)。The coerced value (with appropriate type).

Inheritance
CoerceValueCallback

Examples

下列範例包含此回呼的執行,以根據其他輸入(例如另一個屬性的值)強制轉型相依性屬性的儲存值。The following example includes an implementation of this callback to coerce the stored value of a dependency property based on other inputs, such as another property's value. 在此情況下,回呼會檢查 ShirtType 屬性是否對應至具有按鈕的襯衫類型;如果是,它會為 ButtonColor建立起始預設色彩,如果襯衫型別沒有按鈕,它會將 ButtonColor 值轉回起始值,這會導致 UIUI (未顯示)從有效的選擇中移除該下拉式清單。In this case, the callback checks to see whether the ShirtType property corresponds to a type of shirt that has buttons; if so it establishes a starting default color for the ButtonColor, if the shirt type has no buttons, it coerces the ButtonColor value back to a starting value, which causes the UIUI (not shown) to remove that dropdown from the effective choices.

private static object CoerceButtonColor(DependencyObject d, object value)
{
    ShirtTypes newShirtType = (d as Shirt).ShirtType;
    if (newShirtType == ShirtTypes.Dress || newShirtType == ShirtTypes.Bowling)
    {
        return ButtonColors.Black;				
    }
    return ButtonColors.None;
}
Private Shared Function CoerceButtonColor(ByVal d As DependencyObject, ByVal value As Object) As Object
    Dim newShirtType As ShirtTypes = (TryCast(d, Shirt)).ShirtType
    If newShirtType = ShirtTypes.Dress OrElse newShirtType = ShirtTypes.Bowling Then
        Return ButtonColors.Black
    End If
    Return ButtonColors.None
End Function

Remarks

CoerceValueCallback 為基礎的回呼可以透過數種不同的技術來指派給相依性屬性。Callbacks based on CoerceValueCallback can be assigned to a dependency property through several different techniques. 這些技術都需要您先建立新的屬性中繼資料物件(PropertyMetadata或衍生類別,例如 FrameworkPropertyMetadata)。Each of these techniques requires that you first create a new property metadata object (PropertyMetadata, or a derived class such as FrameworkPropertyMetadata). 使用採用 coerceValueCallback 參數的函式簽章建立中繼資料物件,並將該參數指派給您的回呼處理常式。Create the metadata object using a constructor signature that takes the coerceValueCallback parameter, and assign that parameter to your callback handler. 或依任何簽章來建立中繼資料,並在將中繼資料置於使用中之前,設定 CoerceValueCallback 屬性。Or construct the metadata by any signature and set the CoerceValueCallback property prior to putting the metadata in use.

當您擁有此中繼資料時,您可以:When you have this metadata, you can:

此回呼的執行應檢查 baseValue 中的值,並根據值或類型判斷這是否為需要進一步強制轉型的值。Implementations of this callback should check the value in baseValue and determine based on either the value or the type whether this is a value that needs to be further coerced.

每當屬性系統或任何其他呼叫端呼叫 DependencyObject 實例上的 CoerceValue 時,就會叫用相依性屬性的 CoerceValueCallback,並將該屬性的識別碼指定為 dpThe CoerceValueCallback for a dependency property is invoked any time that the property system or any other caller calls CoerceValue on a DependencyObject instance, specifying that property's identifier as the dp.

屬性值的變更可能來自于屬性系統中任何可能的參與者。Changes to the property value may have come from any possible participant in the property system. 這包括樣式、一般失效、觸發程式、屬性值繼承和本機值設定。This includes styles, generic invalidation, triggers, property value inheritance, and local value setting.

一般來說,您應該避免為任何指定的相依性屬性指定一個以上的 CoerceValueCallback (針對已有 CoerceValueCallback的相依性屬性,以新的中繼資料覆寫或加入)。Generally you should avoid specifying more than one CoerceValueCallback for any given dependency property (overriding or adding with new metadata for a dependency property that already had a CoerceValueCallback). 只有其中一個回呼才能夠採取行動。作用中的回呼將會是在繼承中套用至最衍生的類別(相較于 DependencyObject 呼叫端)。Only one of the callbacks will be able to act. The acting callback will be the one that was applied to the most derived class in the inheritance as compared to the DependencyObject caller. 當覆寫中繼資料時,會取代指派給擁有者階層中較高層級之相依性屬性中繼資料的其他回呼。Other callbacks as assigned to metadata for the dependency property as it existed higher in the owner hierarchy are replaced when the metadata is overridden.

Extension Methods

GetMethodInfo(Delegate)

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

Applies to

See also