Kolumny generowane automatycznie
Tabela danych: DataGrid i ObservableCollection - Aplikacje desktopowe: WPF krok po kroku (wpf)
Bez
AutoGenerateColumns="False" DataGrid sam tworzy kolumnę dla każdej publicznej właściwości, z nazwą właściwości w nagłówku (Name, Grade, Passed), a właściwość typu bool pokazuje jako pole wyboru. Pola klasy bez { get; set; } w tabeli się nie pojawią. Pod tabelą jest pusty wiersz na nową pozycję, bo tabela nie jest tylko do odczytu.MainWindow.xaml
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Uczniowie" Height="220" Width="300">
<StackPanel Margin="10">
<DataGrid x:Name="studentsGrid" Height="140"/>
</StackPanel>
</Window>MainWindow.xaml.cs
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApp
{
public class Student
{
public string Name { get; set; }
public int Grade { get; set; }
public bool Passed => Grade >= 2;
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
students.Add(new Student { Name = "Ala", Grade = 5 });
students.Add(new Student { Name = "Bartek", Grade = 4 });
students.Add(new Student { Name = "Celina", Grade = 6 });
studentsGrid.ItemsSource = students;
}
private readonly ObservableCollection<Student> students = new ObservableCollection<Student>();
}
}Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.
Wszystkie lekcje kursu Aplikacje desktopowe: WPF krok po kroku·Zaloguj się, aby zapisać postęp na koncie.