Home > WPF > Custom MarkupExtension && Nested Extensions == Bug

Custom MarkupExtension && Nested Extensions == Bug

April 16th, 2008

I’m currently working on a custom markup extension and came over a pretty nasty issue. Here’s the working XAML of a dummy extension:

<TextBox Name="txtCity"
   Foreground="{local:ColorExtension Color=Red}"
/>

 

This works like a charm - the fore color is set to red as expected. However - as soon as I try to set the Color property through a resource, I’m getting a compiler error:

<TextBox Name="txtCity"
   Foreground="{local:ColorExtension Color={StaticResource ErrorBrush}}"
/>

Here’s the error message: Unknown property ‘Color’ for type ‘MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension’ encountered while parsing a Markup Extension.

 

Well, the property does exist. Fortunately, Beatriz Costa referred to this bug in her blog - in October 2006. I’m working here with VS2008, targeting .NET 3.5, so I can honestly say: I’m not amused. Fortunately, there’s a workaround: Skip attribute syntax and fall back to property element syntax:

<TextBox Name="txtCity">
  <TextBox.Foreground>
    <local:ColorExtension Color="{StaticResource ErrorBrush}" />
  </TextBox.Foreground>
</TextBox>

 

Another solution would be to assign a constructor to the ColorExtension markup extension that takes the Brush as a parameter. In that case, you could write XAML like this:

<TextBox Name="txtCity"
   Foreground="{local:ColorExtension {StaticResource ErrorBrush}}"
/>

The compiler accepts a StaticResource as a constructor parameter, but it appears it breaks the VS designer. So for now, it’s property element syntax.

 

Some other observations: Some extensions can be nested, others can’t. Using DynamicResource also fails to compile, while using a RelativeSource statement or something like {x:Null} works without complaints.

If somebody can shed some light on this, I’ll be happy to update this post accordingly :)

Tags: ,
  1. December 2nd, 2008 at 06:45 | #1

    Hi Philipp,

    How did such a fundamental problem make production :(

  2. Chuanyu.Wang
    May 20th, 2009 at 12:13 | #2

    You did a great job, this damn bug cost me thw whole afternoon. I am very glad finding the real reason.
    And I found out the third solution. Put your Custom MarkupExtension into a differnt project. How weird it is.

  3. rh
    May 25th, 2009 at 20:25 | #3

    You’ll be frustrated to know this bug is still alive and well in VS2010 Beta1.. *sigh*…

    Thank you for the workaround on this, and thank you also for the BindingDecoratorBase that should have come with WPF in the first place. :)

  4. Clint
    December 23rd, 2009 at 16:40 | #4

    Hi Philipp,

    Ok this cost me at least a day but I managed to get attribute syntax to work by putting the custom markup extensions into a separate assembly…

    Please don’t ask me why it works for me but it does - attribute syntax and all!

    HTH
    Clint

  1. January 4th, 2009 at 05:08 | #1
  2. May 28th, 2009 at 00:52 | #2
  3. November 7th, 2009 at 18:38 | #3