15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

742 ❘ ChaPTer 27 COre xAml<br />

This resource can be referenced by using the StaticResourceExtension with attribute syntax to set the<br />

Background property of a TextBlock. Attribute syntax is defined by curly brackets <strong>and</strong> the name of<br />

the extension class without the Extension postfix.<br />

<br />

The longer form of the attribute shorth<strong>and</strong> notation uses element syntax, as the next code snippet<br />

demonstrates. StaticResourceExtension is defined as a child element of the TextBlock.Background<br />

element. The property ResourceKey is set with an attribute to gradientBrush1. In the previous example,<br />

the resource key is not set with the property ResourceKey (which would be possible as well) but with a<br />

constructor overload where the resource key can be set.<br />

<br />

<br />

<br />

<br />

<br />

Creating Custom markup extensions<br />

A markup extension is created by defining a class that derives from the base class MarkupExtension. Most<br />

markup extensions have the Extension postfix (this naming convention is similar to the Attribute postfix<br />

with attributes, which you can read about in Chapter 14, “Reflection”). With a custom markup extension,<br />

you only need to override the method ProvideValue(), which returns the value from the extension. The<br />

type that is returned is annotated to the class with the attribute MarkupExtensionReturnType. With the<br />

method ProvideValue, an IServiceProvider object is passed. With this interface you can query for<br />

different services, such as IProvideValueTarget or IXamlTypeResolver. IProvideValueTarget can be<br />

used to access the control <strong>and</strong> property where the markup extension is applied to with the TargetObject<br />

<strong>and</strong> TargetProperty properties. IXamlTypeResolver can be used to resolve XAML element names to<br />

CLR objects. The custom markup extension class CalculatorExtension defines the properties X <strong>and</strong> Y<br />

of type double <strong>and</strong> an Operation property that is defined by an enumeration. Depending on the value of<br />

the Operation property, different calculations are done on the X <strong>and</strong> Y input properties, <strong>and</strong> a string is<br />

returned.<br />

using System;<br />

using System.Windows;<br />

using System.Windows.Markup;<br />

namespace Wrox.ProCSharp.XAML<br />

{<br />

public enum Operation<br />

{<br />

Add,<br />

Subtract,<br />

Multiply,<br />

Divide<br />

}<br />

[MarkupExtensionReturnType(typeof(string))]<br />

public class CalculatorExtension : MarkupExtension<br />

{<br />

public CalculatorExtension()<br />

{<br />

}<br />

public double X { get; set; }<br />

public double Y { get; set; }<br />

public Operation Operation { get; set; }<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!