February 16, 2012

Data Templates

Taken from Top Ten UI Development Breakthroughs In Windows Presentation Foundation

You don't have to set the data template on a particular control. If you prefer, you can associate a data template with a particular type, indicating that WPF should use the specified template whenever it encounters that particular type as content.
<DataTemplate DataType="{x:Type local:Person}"> <StackPanel Orientation="Horizontal"> <TextBlock FontWeight="Bold">Name:</TextBlock>
<TextBlock Text="{Binding Name}" Margin="2,0,10,0" /> <TextBlock FontWeight="Bold">
Date of birth:</TextBlock> <TextBlock Text="{Binding DateOfBirth}"Margin="2,0,10,0" /> </StackPanel> </DataTemplate> 
Anything in the window that uses a Person object as its content will pick up this data template by default.

No comments: