How do I create a MarkupExtension with non-string parameters?
I am working on Custom MarkupExtension in WPF Application. Every documented example I've seen uses string parameters from XAML to create a new object. Can I use a non-string parameter?
In other words, how can I do something like this?
[MarkupExtensionReturnType(typeof(Uri))]
public class RefPackUriExtension : MarkupExtension
{
object _assembly = null;
public RefPackUriExtension() { }
public RefPackUriExtension(object assembly)
{
this._assembly = assembly;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
//return an object by using the "_assembly" member somehow
}
}
source to share
Any parameters of yours MarkupExtension
are subject to the same behavior analysis as properties of CLR objects. You can use TypeConverter
to let the user provide string
which has been converted to the target type, or you can use a different one MarkupExtension
.
For an example of the former, see class ColorConverter
. For an example of the latter, see Class RelativeSource
(which is used in Binding
MarkupExtension
).
source to share