1

我正在使用下面写的 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;
4

1 回答 1

4

您可以设置AutoGenerateColumnsfalse,并添加所需的列。

<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" Grid.Row="1" Name="gridPatients" >
 <ItemTemplate>
   <DataTemplate>
      <DataGridTextColumn Binding={ Path=FirstName}/>
    ... 
    <DataTemplate>
 </ItemTemplate>
</DataGrid>
于 2013-01-10T16:50:01.570 回答