views:

933

answers:

2

Has anyone ever created a custom markup extension in WPF or Silverlight? When would you ever want or need to do this? Any tips or sources on how to do it?

+2  A: 

Yes it is handy and I have created one myself. I created a markup extension called EvalBinding that takes a set of bindings as children and a C# evaluation string. It evaluates the C# to process the values from the child bindings so that I do not need to create many simple TypeConverter classes.

For example I can do this...

<EvalBinding Eval="(this[0] > this[1] ? 'GT' : 'LTE')">
    <Binding ElementName="element1" Path="Size"/>
    <Binding ElementName="element2" Path="Size"/>
<EvalBinding>

Where this is a reference to the array of child binding results.

For resources on implementing a MarkupExtension...

MSDN

Josh Smith Blog Entry

Rob Relyea Blog Entry

Phil Wright
+4  A: 

Another example would be for Localization

Note: You can not write custom markup extensions in silverlight.

Olle
Ah, another in it's many limitations. I can't wait for Mix so I can figure out if they fixed this shit.
Charles Graham