แก้ไข

แชร์ผ่าน


IProvideValueTarget Interface

Definition

Represents a service that reports situational object-property relationships for markup extension evaluation.

public interface class IProvideValueTarget
public interface IProvideValueTarget
type IProvideValueTarget = interface
Public Interface IProvideValueTarget

Remarks

IProvideValueTarget represents a service that reports information regarding the object-property structure surrounding where a value converter is invoked.

Type converters and markup extensions can query for IProvideValueTarget as a service on the service context that is provided by a XAML object writer, when the XAML object writer invokes value conversion behavior. For more information, see Type Converters and Markup Extensions for XAML and Service Contexts Available to Type Converters and Markup Extensions.

For markup extensions specifically, a scenario where IProvideValueTarget is useful is to determine the validity of your markup extension's situational usage. For example, your markup extension might implement a value deferral mechanism, but using that mechanism might only be appropriate if the target object is capable of using other components of your deferral mechanism at run time. You could check the type system characteristics of the object returned as TargetObject and make sure that object supports your deferral technique. If so, your markup extension returns a value, and if not, your markup extension throws an exception that will be picked up as an inner exception by a XAML object writer. A similar scenario is assuring that a particular property supports a capability, by checking type system characteristics of the TargetProperty.

Although the API name IProvideValueTarget might imply an association with MarkupExtension.ProvideValue, the information reported by IProvideValueTarget can also be useful to a TypeConverter or ValueSerializer. A TypeConverter or ValueSerializer typically have more context than a markup extension because they are dedicated to a particular target type or target property, and thus the situational validity is usually predictable in the type system. However, other scenarios exist. The following is a list of some scenarios that might be implemented by either a type converter or a markup extension:

  • Get TargetObject to read instance values of other properties on that object. Note that your handling here might need to be robust for unset values, because the processing order of XAML members on the target object can be nondeterministic.

  • Get TargetObject to read type-level metadata, or to verify that required interfaces are implemented by the type.

  • Get TargetProperty to read property metadata.

One scenario is to use IProvideValueTarget information to limit which objects or properties are permitted to be set by your markup extension.

Before attempting to use the service, make sure that the service itself is not null when returned by the relevant service provider parameter you are using for context.

Important

When using the service, it is particularly important not to modify the object referenced by the TargetObject property, or other objects referenced by that object. For WPF specifically, these changes could cause unexpected changes that would invalidate the WPF property system.

Note

Do not use the type of TargetProperty to determine which precise type a markup extension or type converter should return. That is not a best practice. Your return type should be consistent with attributing (MarkupExtensionReturnTypeAttribute for markup extensions) or patterns (class name of the type converter).

In previous versions of the .NET Framework, this interface existed in the WPF-specific assembly WindowsBase. In .NET Framework 4, IProvideValueTarget is in the System.Xaml assembly. For more information, see Types Migrated from WPF to System.Xaml.

WPF Usage Notes

This service supports framework features such as binding on a dependency property.

In WPF, the service represented by IProvideValueTarget can return objects and properties for situations where the value to return from a markup extension or type converter depends on context.

IProvideValueTarget is leveraged heavily in the internal implementation of BindingBase, the class that implements the binding mechanism for dependency properties in WPF.

A default internal service provider for WPF implements IProvideValueTarget for usage by a custom markup extension or type converter. The object and property reported by IProvideValueTarget can only be set by the internal WPF service. Going beyond this definition of how IProvideValueTarget operates would require defining your own service provider for markup extension and type converter usages that implements at the very least the IProvideValueTarget and IXamlTypeResolver services, and that is an advanced scenario that is not described here.

A null value for TargetProperty or TargetObject does not necessarily mean that a markup extension or type converter cannot act, although that depends on your implementation. An example case where the values from IProvideValueTarget are null even if the service is available is if your markup extension is nested within another markup extension, such as a Binding. An implementation that relies on IProvideValueTarget should always check for null values if only for the purpose of throwing a specific exception, if that is your intended design.

Properties

TargetObject

Gets the target object being reported.

TargetProperty

Gets an identifier for the target property being reported.

Applies to

See also