ServiceProvider and XAML: type converters and markup extensions
Internal Mail thread (read from bottom)…shared to make the info spread fastest…
Not sure how well I’ve speced/documented this. Can you please add to SDK?
Basically, we should make sure that we document that service provider is available inside a type converter ConvertFrom call and inside ProvideValue.
We provide 3 different interfaces that you can GetService from the service provider.
Here are the concrete examples of how we use this mechanism in our code base…(examples are simplified a bit)
Example of using IProvideValueTarget – similar to how Binding does this:
public sealed override object ProvideValue(IServiceProvider serviceProvider)
if (serviceProvider != null)
IProvideValueTarget target1 = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
Object obj1 = target1.TargetObject;
Object property1 = target1.TargetProperty; //might be a propertyInfo or DependencyProperty
Example of using IXamlTypeResolver inside a ProvideValue call for TypeExtension (supports x:Type)
public override object ProvideValue(IServiceProvider serviceProvider)
string typeName = "foo:Button";
IXamlTypeResolver resolver1 = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
if (resolver1 == null)
if (type == null)
Example of using IUriContext from inside a TypeConverter called during XamlReader or Baml load.
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
IUriContext context1 = (IUriContext) context.GetService(typeof(IUriContext));
if (context1.BaseUri != null)
Uri baseUri = context1.BaseUri
Rob, as far as I can see this isn’t documented in MSDN. MSDN documents that MarkupExtension.ProvideValue takes an IServiceProvider, but doesn’t say what it is or does. It also documents IProvideValueTarget, but says only (cryptically, in my opinion): “Reports object-property relationships for framework features such as binding on a dependency property”. It also warns you off with this: “This interface is used internally by some WPF types, and should not be used by application or type authors.”
If third parties are supposed to be able to implement markup extensions (are they, by the way?) this would need to be better documented.