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.

1010 ❘ ChaPTer 35 cOre wpf<br />

multiTrigger<br />

A property trigger fires when a value of a property changes. If you need to set a trigger because two or more<br />

properties have a specific value, you can use MultiTrigger.<br />

MultiTrigger has a Conditions property where valid values of properties can be specified. It also has<br />

a Setters property, where the properties that need to be set can be specified. In the example, a style is<br />

defined for TextBox elements where the trigger applies if the IsEnabled property is True <strong>and</strong> the Text<br />

property has the value Test. If both apply, the Foreground property of the TextBox is set to Red:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

data Triggers<br />

code snippet TriggerDemo/MultiTriggerWindow.xaml<br />

Data triggers fire if bound data to a control fulfills specific conditions. In the following example, a Book<br />

class is used that has different displays depending on the publisher of the book.<br />

The Book class defines the properties Title <strong>and</strong> Publisher <strong>and</strong> has an overload of the ToString()<br />

method:<br />

public class Book<br />

{<br />

public string Title { get; set; }<br />

public string Publisher { get; set; }<br />

}<br />

public override string ToString()<br />

{<br />

return Title;<br />

}<br />

code snippet TriggerDemo/Book.cs<br />

In the XAML code, a style is defined for ListBoxItem elements. The style contains DataTrigger elements<br />

that are bound to the Publisher property of the class that is used with the items. If the value of the<br />

Publisher property is Wrox Press, the Background is set to Red. With the publishers Dummies <strong>and</strong><br />

Sybex, the Background is set to Yellow <strong>and</strong> LightBlue accordingly:<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!