CoerceValueCallback 代理人

定義

提供方法的範本,該方法會在相依性屬性值重新評估時或特別要求強制型轉 (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 

參數

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.

傳回值

Object

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

繼承
CoerceValueCallback

範例

下列範例包含此回呼的執行, 以根據其他輸入 (例如另一個屬性的值) 強制轉型相依性屬性的儲存值。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

備註

以為基礎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.

CoerceValueCallback屬性系統或任何其他呼叫端在DependencyObject實例上呼叫時dp, 會叫CoerceValue用相依性屬性的, 將該屬性的識別碼指定為。The 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.

擴充方法

GetMethodInfo(Delegate)

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

適用於

另請參閱