I love DataTemplates
Posted August 18, 2009
By joshua
I have been playing around with WPF and started writing a little game to experiment with. So far I think my favorite thing about WPF has been data templates. I defined a data template for a Gem Card, one of the game pieces.
<DataTemplate DataType="{x:Type game:GemCard}">
<Border Style="{DynamicResource CardBorder}" Background="Green">
<Border Style="{DynamicResource CardHighlight}">
<Grid>
<Label Content="{Binding Value}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" FontSize="18.667"/>
<Label Content="{Binding Value}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="5" FontSize="18.667"/>
<Path Stretch="Fill" Stroke="Black" Data="m0,0 l1,1 -1,1 -1,-1z" Fill="Red" HorizontalAlignment="Center" VerticalAlignment="Center" Width="40" Height="60"/>
<Label Content="{Binding Gems}" FontSize="18.667" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</Border>
</DataTemplate>
And when I put a GemCard in a content control in my game it looks something like this
I like this approach a lot because I can just tell WPF how I want my classes to be presented and then I can just start putting them in listboxes with out having to worry about how the will look.