WPF 4 DataGrid Control (Advanced chapter II)

Source: Internet
Author: User
Tags datetime

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>

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.