wpf中DataGrid的样式

目录

一、简单使用风格

二、增加Columns的类型

三、显示行细节 


在wpf中DataGrid是要经常遇到的,也是一个非常复杂的控件。本节使用DataGrid建立一个表格,并且带有上下左右的滚动条,以及表格中有按钮的功能。

一、简单使用风格

1.首先建立一个wpf程序,文件总览

wpf中DataGrid的样式_第1张图片

2.MainWindow.xaml文件代码


    

        
            
                
                
                
                
                
                
                    
                        
                            
                                
                            
                        
                    
                
            
        

    

3.后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataGrid
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List users = new List();
            users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23), Sex=1, School="A" });
            users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17), Sex = 0, School = "B" });
            users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2), Sex = 1, School = "C" });
            users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23), Sex = 1, School = "A" });
            users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17), Sex = 0, School = "B" });
            users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2), Sex = 1, School = "C" });
            users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23), Sex = 1, School = "A" });
            users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17), Sex = 0, School = "B" });
            users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2), Sex = 1, School = "C" });

            dgSimple.ItemsSource = users;
        }
    }

    public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public DateTime Birthday { get; set; }
        public int Sex { get; set; }

        public string School { get; set; }
    }
}

4.Button样式


    

5.DataGrid样式


    
    

    
    
    
    
    

    
    

6.上下左右滚动条ScrollViewer样式