Binding to a MarkupExtension that Returns a Binding

Is it possible to bind properties to a MarkupExtension that returns a Binding? Yes! The MarkupExtension just needs to return a BindingExpression by returning binding.ProvideValue(serviceProvider):


    public class PhysicalLengthMarkupExtension : MarkupExtension


        public override object ProvideValue(IServiceProvider serviceProvider)


            Binding physicalLengthBinding = new Binding();

            physicalLengthBinding.Source = _dpiProvider;

            physicalLengthBinding.Path = new PropertyPath("DPI");

            physicalLengthBinding.Converter = _dpiConverter;

            physicalLengthBinding.ConverterParameter = _length;

            return physicalLengthBinding.ProvideValue(serviceProvider);



Which enables the ability to write

<TextBlock Width="{Markup:PhysicalLengthMarkupExtension 3cm}"/>


instead of the more verbose and redundant



< Binding

                  Source="{StaticResource DpiProvider}"


                  Converter="{StaticResource DpiConverter}"






(Btw, I know that WPF takes DPI into account when doing layout calculations but let’s just use it as an example here.)