Looking at Triggers with Styles and Control Templates - Data Triggers
(Page 4 of 4 )
Unlike property triggers, which check only WPF dependency properties, data triggers can check any old .NET object property. While property triggers are generally used to check WPF visual-element properties, data triggers are normally used to check the properties of non-visual objects used as content, such as our PlayerMove objects in Example 5-28.
Example 5-28. Two data triggers
<Window.Resources>
<Style TargetType="{x:Type Button}">
...
</Style>
<Style x:Key="CellTextStyle" TargetType="{x:Type TextBlock}">
...
<Style.Triggers>
<DataTrigger Binding="{Binding Path=PlayerName}" Value="X">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=PlayerName}" Value="O">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="MoveNumberStyle" TargetType="{x:Type TextBlock}">
...
</Style>
...
<DataTemplate DataType="{x:Type l:PlayerMove}">
<Grid>
<TextBlock
TextContent="{Binding Path=PlayerName}"
Style="{StaticResource CellTextStyle}" />
<TextBlock
TextContent="{Binding Path=MoveNumber}"
Style="{StaticResource MoveNumberStyle}" />
</Grid>
</DataTemplate>
</Window.Resources>
DataTriggerelements go under theStyle.Triggerselement just like property triggers and, just like property triggers, there can be more than one of them active at any one time. While a property trigger operates on the properties of the visual elements displaying the content, a data trigger operates on the content itself. In our case, the content of each of the cells is aPlayerMove object. In both of our data triggers, we’re binding to thePlayerName property. If the value isX, we’re setting the foreground to red and if it’sO, we’re setting it to green.
Take care where you put the data trigger. In our example, we’ve got theButton-type style and the namedCellTextStylestyle as potential choices. I’ve written this chapter twice now and both times I’ve initially put the data trigger on the button style instead of on the content in the data template. Data triggers are based on content, so make sure you put them into your content styles, not your control styles.
We haven’t had per-player colors since we moved to data templates after setting styles programmatically in Figure 5-5, but data triggers bring us that feature right back, along with all of the other features we’ve been building up, as shown in Figure 5-12.
Unlike property triggers, which rely on the change notification of dependency properties, data triggers rely on an implementation of the standard property-change notification patterns that are built into .NET and are discussed in Chapter 4—e.g.,INotifyPropertyChanged. Since eachPlayerMove object is constant, we don’t need to implement this pattern, but if you’re using data triggers, chances are that you will need to implement it on your custom content classes.
One other especially handy feature of data triggers is that there’s no need for an explicit check for null content. If the content is null, the trigger condition is automatically false, which is why the application isn’t crashing trying to dereference a nullPlayerMoveto get to thePlayerNameproperty.

Figure 5-12. Data triggers in action
Please check back next week for the conclusion to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter five of the book Programming Windows Presentation Foundation, written by Chris Sells and Ian Griffiths (O'Reilly; ISBN: 0596101139). Check it out today at your favorite bookstore. Buy this book now.
|
|