using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using NavTest.Eneities; using NavTest.Service.Ioc; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows.Controls; using System.Windows.Media; namespace NavTest.ViewModels { public partial class Page5ViewModel : ObservableObject, ISingleton { private readonly PLCModels pLCModelsIoc; public Page5ViewModel(PLCModels pLCModelsIoc) { this.pLCModelsIoc = pLCModelsIoc; this.PLCModels = pLCModelsIoc.pLCModels; MyBrush = new SolidColorBrush(Colors.Red); for (int i = 0; i < 50; i++) { MyStudents.Add( new() { Id = i, Age = $"{i + 10}", Name = $"Name{i + 5}", MyCourse = new() { new Course() { CourseName = "语文", Teacher = "张老师", Score = i + 60, Classroom = $"教室{i}" }, new Course() { CourseName = "英语", Teacher = "李老师", Score = i + 61, Classroom = $"教室{i}" } , new Course() { CourseName = "数学", Teacher = "张老师", Score = i + 60, Classroom = $"教室{i}" } , new Course() { CourseName = "语文", Teacher = "张老师", Score = i + 60, Classroom = $"教室{i}" } , new Course() { CourseName = "语文", Teacher = "张老师", Score = i + 60, Classroom = $"教室{i}" } } } ); } } [ObservableProperty] private ObservableCollection pLCModels; [ObservableProperty] private Brush myBrush; [ObservableProperty] private List myStudents = new(); [ObservableProperty] private int detailInex; [RelayCommand] public void Detail(DataGrid dataGrid) { DataGridRow dataGridRow = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(DetailInex); if (dataGridRow.DetailsVisibility == System.Windows.Visibility.Visible) { dataGridRow.DetailsVisibility = System.Windows.Visibility.Collapsed; } else { dataGridRow.DetailsVisibility = System.Windows.Visibility.Visible; } } } }
public class Student { public int Id { get; set; } public string? Name { get; set; } public string? Description { get; set; } public string? Age { get; set; } public List? MyCourse { get; set; } }
public class Course { public string? CourseName { get; set; } public string? Teacher { get; set; } public int? Score { get; set; } public string? Classroom { get; set; } }
效果: