Lista z ItemsSource i SelectedItem

Wiązanie danych: DataContext i Binding - Aplikacje desktopowe: WPF krok po kroku (wpf)

ItemsSource="{Binding Students}" bierze pozycje listy z modelu, a DisplayMemberPath="Name" mówi, którą właściwość pokazać. SelectedItem wiąże się w dwie strony z Selected w modelu, więc napis z {Binding Selected.Grade} pokazuje ocenę zaznaczonego ucznia. Kolekcja ObservableCollection powiadamia listę o dodanych uczniach.

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="Klasa" Height="260" Width="280">
    <StackPanel Margin="10">
        <ListBox x:Name="studentList" ItemsSource="{Binding Students}" DisplayMemberPath="Name" Height="90" SelectedItem="{Binding Selected}"/>
        <TextBlock x:Name="gradeText" Margin="0,6,0,0" Text="{Binding Selected.Grade, StringFormat=Ocena: {0}}"/>
        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
            <TextBox x:Name="nameBox" Width="120"/>
            <Button x:Name="addButton" Content="Dodaj ucznia" Margin="6,0,0,0" Click="AddButton_Click"/>
        </StackPanel>
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
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 class ClassModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        private Student selected;

        public ObservableCollection<Student> Students { get; } = new ObservableCollection<Student>
        {
            new Student { Name = "Ala", Grade = 4 },
            new Student { Name = "Bartek", Grade = 5 },
            new Student { Name = "Celina", Grade = 3 },
        };

        public Student Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                OnPropertyChanged();
            }
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = model;
        }

        private readonly ClassModel model = new ClassModel();

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            model.Students.Add(new Student { Name = nameBox.Text, Grade = 1 });
            nameBox.Clear();
        }
    }
}

Ł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.