Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markup Extensions in WPF/Silverlight

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?

like image 795
Charles Graham Avatar asked Mar 04 '09 01:03

Charles Graham


3 Answers

An example would be for Localization:

A simple and effective way to localize application resources is to write a custom MarkupExtension that provides a localized value. The extension takes a parameter that is a unique resource key... [and then] looks up the value from a generic resource provider.

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

like image 92
Olle Avatar answered Oct 20 '22 22:10

Olle


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

like image 29
Phil Wright Avatar answered Oct 21 '22 00:10

Phil Wright


Hooray!!

This is implemented in Silverlight 5!!

And furthermore, now it's a generic interface instead of a class!!

Check it out.

Read this for an example.

like image 23
Shimmy Weitzhandler Avatar answered Oct 20 '22 22:10

Shimmy Weitzhandler