WPF 创建DataGrid 样式

76 篇文章 1 订阅

先展示一下样式效果,如下:

UI XAML代码如下:

<Window x:Class=“wpf_DataGird_Test.MainWindow”

xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”

xmlns:d=“http://schemas.microsoft.com/expression/blend/2008”

xmlns:mc=“http://schemas.openxmlformats.org/markup-compatibility/2006”

xmlns:local=“clr-namespace:wpf_DataGird_Test”

mc:Ignorable=“d”

Title=“DataGrid Demo1” Height=“450” Width=“800”>

<DataGrid x:Name=“dg”  Margin=“5”

AutoGenerateColumns=“False”

LoadingRow=“dg_LoadingRow”

CanUserAddRows=“False”

RowHeaderWidth=“20”>

<DataGrid.ColumnHeaderStyle>

</DataGrid.ColumnHeaderStyle>

<DataGrid.Columns>

<DataGridTextColumn Binding="{Binding Name}" IsReadOnly=“True” Header=“Author Name”

Width=“160” FontFamily=“Georgia” FontSize=“18” FontWeight=“Black” SortDirection=“Descending”/>

</DataGrid.Columns>

在后台代码中添加数据模型:

public class Author

{

public int ID { get; set; }

public string Name { get; set; }

public DateTime DOB { get; set; }

public string BookTitle { get; set; }

public bool IsMVP { get; set; }

public string Email { get; set; }

public string City { get; set; }

public string Country { get; set; }

public int Zip { get; set; }

}

窗口页面的后台代码:

namespace wpf_DataGird_Test

{

///

/// MainWindow.xaml 的交互逻辑

///

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

dg.ItemsSource = CreateDemoSource();

}

// 

private void dg_LoadingRow(object sender, DataGridRowEventArgs e)

{

e.Row.Header = (e.Row.GetIndex() + 1).ToString();

}

//创建数据源

private List CreateDemoSource()

{

List authors = new List();

authors.Add(new Author()

{

ID = 101,

Name = “Mahesh Chand”,

BookTitle = “Graphics Programming with GDI+”,

DOB = new DateTime(1975, 2, 23),

IsMVP = false

});

authors.Add(new Author()

{

ID = 201,

Name = “Mike Gold”,

BookTitle = “Programming C#”,

DOB = new DateTime(1982, 4, 12),

IsMVP = true

});

authors.Add(new Author()

{

ID = 244,

Name = “Mathew Cochran”,

BookTitle = “LINQ in Vista”,

DOB = new DateTime(1985, 9, 11),

IsMVP = true

});

return authors;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值