In the previous WPF 4 DataGrid control (Advanced one), we edited the style of the date column through the Datagridtemplatecolumn class customization, and, of course, you can set any style template to suit your individual needs. In the previous example, pass Exam showed whether the student passed the exam, but we did not know how much the student had achieved in each subject. This article adds this detail to the DataGrid row, making the DataGrid data more fulfilling.
First, we'll still update the member class to add math and history two disciplines:
public class Member
{
public string Name { get; set; }
public string Age { get; set; }
public SexOpt Sex { get; set; }
public bool Pass { get; set; }
public DateTime ExamDate { get; set; }
public Uri Email { get; set; }
public int Math { get; set; }
public int History { get; set; }
}
Test scores for students:
… …
memberData.Add(new Member()
{
Name = "Lucy",
Age = "25",
Sex = SexOpt.Female,
Pass = true,
ExamDate = new DateTime(2010, 4, 10),
Email = new Uri("mailto:Lucy@school.com"),
Math = 80,
History = 85
});
dataGrid.DataContext = memberData;
The next step is to design a style template for test scores in XAML:
<Window.Resources>
... ...
<DataTemplate x:Key="RowDetails">
<Border BorderThickness="0" Background="Orchid" Padding="10">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Math: " VerticalAlignment="Center"/>
<TextBlock Text="{Binding Math}" VerticalAlignment="Center"
FontSize="15" FontWeight="Bold"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="History: " VerticalAlignment="Center"/>
<TextBlock Text="{Binding History}" VerticalAlignment="Center"
FontSize="15" FontWeight="Bold"/>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>