In the previous WPF 4 DataGrid control (custom style article), we mastered the DataGrid list header, row header, row, cell-related custom style method, and we continue to make some advanced settings for the custom style.
Datagridtemplatecolumn class
In addition to the column types shown in the following table, we can add a more perfect way to display data for the DataGrid by datagridtemplatecolumn Custom column styles.
First we add examdate to the class to display the student test date in the DataGrid and assign a value to the corresponding student.
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; }
}
… …
ObservableCollection<Member> memberData = new ObservableCollection<Member>();
… …
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")
});
dataGrid.DataContext = memberData;
… …
The next step is to define two DataTemplate style templates (the following code) in <Window.Resources>, the first to set the display of date columns:
<DataTemplate x:Key="DateTemplate" >
<StackPanel Width="40" Height="30">
<Border Background="Orange" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding ExamDate, StringFormat={}{0:MM-dd}}"
FontSize="10" HorizontalAlignment="Center"/>
</Border>
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding ExamDate, StringFormat={}{0:yyyy}}"
FontSize="10" HorizontalAlignment="Center"/>
</Border>
</StackPanel>
</DataTemplate>