CoerceValueCallback CoerceValueCallback CoerceValueCallback CoerceValueCallback Delegate

定義

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

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

baseValue
Object Object Object Object

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

傳回值

System.Object

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

繼承
CoerceValueCallbackCoerceValueCallbackCoerceValueCallbackCoerceValueCallback

範例

下列範例會加入此回撥到其他的輸入,例如另一個屬性的值為基礎的相依性屬性的預存的值強制轉型的實作。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,如果 shirt 型別不有任何按鈕,它會強制使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相依性屬性會叫用屬性系統或任何其他呼叫端呼叫任何時候CoerceValueDependencyObject執行個體,並指定該屬性的識別項為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.

擴充方法

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

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

適用於

另請參閱