我正在使用下面写的 XAML
<Window x:Class="ERP.WinApp.Views.Admin.Patients"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Patients" Height="auto" MinWidth="1024" Width="1024" ShowInTaskbar="False" Icon="/ERP.WinApp;component/Images/patient.png" WindowStartupLocation="CenterScreen">
<Grid>
<DataGrid ItemsSource="{Binding}" Grid.Row="1" Name="gridPatients" ></DataGrid>
</Grid>
</Window>
后面写的代码
namespace ERP.WinApp.Views.Admin
{
/// <summary>
/// Interaction logic for Patients.xaml
/// </summary>
public partial class Patients : Window
{
public Patients()
{
InitializeComponent();
List<Patient> list = new List<Patient>();
list = // Populate it through some method
gridPatients.DataContext = list;
}
}
}
患者类几乎没有简单的属性
public class Patient
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string FullName { get{ return this.FirstName+ " " +this.MiddleName+ " " +this.LastName; } }
public DateTime DOB { get; set; }
public int Age { get { return DateTime.Today.Year - this.DOB.Year; } }
public char Gender { get; set; }
}
当我运行该应用程序时,我的数据网格中的所有列都包含数据,而如果我只想有几列,例如跳过 ID 和年龄以及第一、中间、姓氏,那么最好的方法是什么。
我认为这样做对于我想隐藏的每一列都是不好的方式
gridPatients.Columns[0].Visibility = Visibility.Collapsed;