15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1014 ❘ ChaPTer 35 cOre wpf<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

code snippet TemplateDemo/StyledButtonWindow.xaml<br />

Such a styled button now looks very fancy on the screen. There’s still a problem: there is<br />

no action if the mouse is clicked or the mouse moves over the button. This isn’t the typical<br />

feeling a user has with a button. This can be solved: with a template-styled button, you<br />

must have triggers for the button to appear differently in response to mouse moves <strong>and</strong><br />

mouse clicks.<br />

figure 35-23<br />

Using property triggers (discussed previously), this can be solved easily. The triggers<br />

just need to be added to the Triggers collection of the ControlTemplate as shown.<br />

Here, two triggers are defined. One property trigger is active when the IsMouseOver property of the<br />

button is true. Then the Fill property of the ellipse with the name GelBackground is changed to a<br />

RadialGradientBrush with values from Lime to DarkGreen. With the IsPressed property, other colors<br />

are specified for the RadialGradientBrush:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Now you can run the application <strong>and</strong> see a visual feedback from the button as soon as the mouse hovers<br />

over it or the mouse is clicked.<br />

data Templates<br />

The content of ContentControl elements can be any content — not only WPF elements but also .<strong>NET</strong> objects.<br />

For example, an object of the Country type can be assigned to the content of a Button class. The Country<br />

class has been created to represent the name <strong>and</strong> flag with a path to an image. This class defines the Name <strong>and</strong><br />

ImagePath properties, <strong>and</strong> it has an overridden ToString() method for a default string representation:<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!