WPF 4 DataGrid Control (Advanced Chapter One)

Source: Internet
Author: User
Tags datetime

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.