更改DataGrid列标题文本

编程入门 行业动态 更新时间:2024-10-11 13:22:50
本文介绍了更改DataGrid列标题文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个特定类类型为 Person 的列表,我想用它创建一个 DataGrid 。 / p>

I have a list of a specific class type Person and I want to make a DataGrid with it.

private void DataGrid_Loaded(object sender, RoutedEventArgs e) { List<Person> x; //Don't worry, x has data in it (sender as DataGrid).ItemsSource = x; }

还有 Person class:

class Person { string fName, lName; }

毕竟,我得到了一个带有标题的表: fName和 lName。如何将其更改为:名字和姓氏?

After all this I get a table with the headers: "fName" and "lName". How can I change that to: "First Name" and "Last Name"?

推荐答案

这里是正确的做法:

首先在后面的代码中定义一个 ObservableCollection ,该代码将保存人员列表

First Define an ObservableCollection in the codebehind that will hold a list of persons

第二将该列表绑定到 DataGrid ItemSource 并绑定其属性

Second Bind that list to the DataGrid ItemSource and Bind its properties

您可以通过简单地禁用 AutoGenerateColumns 并自行设置名称来更改显示在每列上的名称

You can change what name to display on each column by simply disabling the AutoGenerateColumns and setting their names by your self

此处为完整代码

<DataGrid ItemsSource="{Binding ListPersons}" AutoGenerateColumns="False"> <DataGrid.Columns > <DataGridTextColumn Header="First Name" Binding="{Binding FName}"></DataGridTextColumn> <DataGridTextColumn Header="Last Name" Binding="{Binding LName}"></DataGridTextColumn> </DataGrid.Columns> </DataGrid>

以及后面的代码:

public class Person { public String FName { get; set; } public String LName { get; set; } } public partial class MainWindow : Window { public ObservableCollection<Person> ListPersons { get; set; } public MainWindow() { ListPersons=new ObservableCollection<Person>() { new Person() { FName = "FName1", LName = "LName1" }, new Person() { FName = "FName2", LName = "LName2" } }; this.DataContext = this; } }

更多推荐

更改DataGrid列标题文本

本文发布于:2023-11-05 18:13:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   标题   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!