在这里的窗体布局中,同时对姓名和格言的表头样式和Cell里面的样式进行了修改。其中DataGridTextColumn.HeaderTemplate包括的是头部的样式;DataGridTextColumn.ElementStyle宝开的是Cell的样式。
定义的实体类 public class UserInformation { public string UserName { get; set; } public string Province { get; set; } public string City { get; set; } public string County { get; set; } public int Age { get; set; } public string Motto { get; set; } } ViewModel类的定义 public class MainViewModel:ViewModelBase { private string _title; public string Title { get { return _title; } set { Set(ref _title, value); } } private ObservableCollection<UserInformation> _userInformations; public ObservableCollection<UserInformation> UserInformations { get { return _userInformations; } set { Set(ref _userInformations, value); } } public MainViewModel() { Title = Guid.NewGuid().ToString(); UserInformations = new ObservableCollection<UserInformation>(new List<UserInformation>() { new UserInformation() { UserName = "Bob",Age = 32,Province = "山东",City = "济南",County = "天桥",Motto = "会当云绝顶,\r\n一览众山小。" }, new UserInformation() { UserName = "Bob",Age = 32,Province = "山东",City = "济南",County = "天桥",Motto = "会当云绝顶,\r\n一览众山小。" }, new UserInformation() { UserName = "Bob",Age = 32,Province = "山东",City = "济南",County = "天桥",Motto = "会当云绝顶,\r\n一览众山小。" } }); } }本实例使用了.netcore 3.1版本进行的开发,但是对于.net framework同样适用。
MVVM模式是使用MVVMLightstd10,可以从nuget上面进行下载。
本示例已上传至GITHUB:https://github.com/mzy666888/MultiHeaderOfDataGridWpfApp