Punkteeingabe: Korrekturmodus wählbar, Bonusaufgaben nicht mitnummeriert
CI / build-and-test (push) Canceled after 0s

- "Enter springt: Schüler für Schüler / Aufgabe für Aufgabe" im Dialog wählbar und gemerkt;
  Umschalt+Enter zurück, Pfeiltasten springen direkt und öffnen das Eingabefeld
- Tasten per Tunnel-Handler abgefangen, bevor DataGrid/TextBox sie auswerten
- Abwesende Schüler und Aufgaben der anderen Fassung werden übersprungen
- Zellen lesen ihre Zeile aus dem DataContext (recycelte Zeilen schreiben nicht mehr falsch)
- Bonusaufgaben zählen bei der angezeigten Nummerierung nicht mit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-01 23:36:12 +02:00
1 parent 6c0a2dbd2e
commit d7898d5678
14 files changed
+419 -145

No files matched your search

@@ -105,7 +105,7 @@
BorderThickness="1" CornerRadius="4" Padding="8" Margin="0,0,0,6">
<StackPanel Spacing="6">
<Grid ColumnDefinitions="26,*,Auto,Auto,Auto,Auto,Auto">
<TextBlock Grid.Column="0" Text="{Binding Nr}" VerticalAlignment="Center" Opacity="0.6"/>
<TextBlock Grid.Column="0" Text="{Binding DisplayNr}" VerticalAlignment="Center" Opacity="0.6"/>
<TextBox Grid.Column="1" Text="{Binding Title}" PlaceholderText="Titel der Aufgabe" Margin="0,0,6,0"/>
<NumericUpDown Grid.Column="2" Value="{Binding MaxPoints}" Minimum="0" FormatString="0.##"
Width="72" ShowButtonSpinner="False" Margin="0,0,6,0" ToolTip.Tip="Maximalpunkte"/>
@@ -18,6 +18,10 @@
<Button Content="Rest als abwesend markieren" Command="{Binding MarkRemainingAbsentCommand}"
Margin="20,0,0,0" VerticalAlignment="Center"
ToolTip.Tip="Setzt bei allen noch leeren Zeilen 'Abwesend' — für Schüler, die nicht nachschreiben."/>
<TextBlock Text="Enter springt:" Margin="20,0,0,0" VerticalAlignment="Center" FontSize="13"/>
<ComboBox ItemsSource="{Binding EntryOrderOptions}" SelectedItem="{Binding EntryOrderName}"
Width="190" VerticalAlignment="Center"
ToolTip.Tip="Schüler für Schüler: eine Arbeit komplett, dann die nächste. Aufgabe für Aufgabe: eine Aufgabe bei allen, dann die nächste. Pfeiltasten bewegen sich immer direkt in die Richtung, Umschalt+Enter geht zurück."/>
</StackPanel>
<DataGrid Grid.Row="1" Name="ResultGrid"
@@ -1,5 +1,6 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
@@ -9,7 +10,7 @@ using Avalonia.Media;
using Avalonia.Threading;
using Avalonia.VisualTree;
using LehrerApp.Desktop.ViewModels.Groups;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
namespace LehrerApp.Desktop.Views.Groups;
@@ -17,21 +18,37 @@ namespace LehrerApp.Desktop.Views.Groups;
public partial class ExamGradingDialog : Window
{
private static readonly IBrush InvalidBackground = new SolidColorBrush(Color.Parse("#FFCDD2"));
private const string PointsCellName = "Pts";
private const string CommentCellName = "Comment";
public ExamGradingDialog() => InitializeComponent();
/// Aufgaben-Spalten in Aufgabenreihenfolge — nicht über einen festen Index-Offset in
/// grid.Columns auflösen, weil bei A/B-Arbeiten noch die "Fassung"-Spalte davor steht.
private readonly List<DataGridColumn> _taskColumns = [];
public ExamGradingDialog()
{
InitializeComponent();
// Tunnel statt KeyDown am TextBox: so kommt die Navigation vor TextBox und DataGrid an
// die Reihe. Vorher hat die DataGrid-eigene Tastenbehandlung Enter/Pfeile teilweise selbst
// ausgewertet (Sprung in die erste Zeile, Zellauswahl ohne offenes Eingabefeld).
AddHandler(KeyDownEvent, OnPreviewKeyDown, RoutingStrategies.Tunnel);
}
private DataGrid? Grid => this.FindControl<DataGrid>("ResultGrid");
protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
if (DataContext is ExamGradingDialogViewModel vm) BuildColumns(vm);
if (DataContext is not ExamGradingDialogViewModel vm || Grid is not { } grid) return;
BuildColumns(vm, grid);
grid.CurrentCellChanged += (_, _) => OnGridCurrentCellChanged(vm, grid);
if (vm.FirstCell() is { } first) FocusPointsCell(vm, first.Row, first.Task);
}
private void BuildColumns(ExamGradingDialogViewModel vm)
private void BuildColumns(ExamGradingDialogViewModel vm, DataGrid grid)
{
var grid = this.FindControl<DataGrid>("ResultGrid");
if (grid is null) return;
grid.Columns.Clear();
_taskColumns.Clear();
grid.Columns.Add(new DataGridTextColumn
{
@@ -45,26 +62,26 @@ public partial class ExamGradingDialog : Window
grid.Columns.Add(new DataGridTemplateColumn
{
Header = "Fassung", Width = new DataGridLength(82, DataGridLengthUnitType.Pixel),
CellTemplate = new FuncDataTemplate<ExamResultRow>((row, _) =>
CellTemplate = new FuncDataTemplate<ExamResultRow>((_, _) =>
{
if (row is null) return new TextBlock();
var choice = new ComboBox { ItemsSource = row.VariantOptions, SelectedItem = row.VariantName };
choice.SelectionChanged += (_, _) => row.VariantName = choice.SelectedItem as string ?? "A";
var choice = new ComboBox();
choice.Bind(ItemsControl.ItemsSourceProperty, new Binding(nameof(ExamResultRow.VariantOptions)));
choice.Bind(SelectingItemsControl.SelectedItemProperty,
new Binding(nameof(ExamResultRow.VariantName)) { Mode = BindingMode.TwoWay });
return choice;
}),
});
for (var i = 0; i < vm.Tasks.Count; i++)
{
var task = vm.Tasks[i];
grid.Columns.Add(new DataGridTemplateColumn
var column = new DataGridTemplateColumn
{
Header = (string.IsNullOrWhiteSpace(task.Title) ? $"Aufg. {task.Nr}" : $"{task.Nr}. {task.Title}")
+ (task.Variant is null ? "" : $" ({task.Variant})")
+ (task.IsBonus ? " ★ Bonus" : ""),
Header = vm.TaskHeaders[i],
Width = new DataGridLength(1, DataGridLengthUnitType.Star),
CellTemplate = BuildPointsCellTemplate(i, grid, vm.Tasks.Count),
});
CellTemplate = BuildPointsCellTemplate(i),
};
_taskColumns.Add(column);
grid.Columns.Add(column);
}
grid.Columns.Add(new DataGridTextColumn
@@ -80,47 +97,76 @@ public partial class ExamGradingDialog : Window
grid.Columns.Add(new DataGridTemplateColumn
{
Header = "Abw.", Width = new DataGridLength(50, DataGridLengthUnitType.Pixel),
CellTemplate = BuildAbsentCellTemplate(),
CellTemplate = new FuncDataTemplate<ExamResultRow>((_, _) =>
{
var cb = new CheckBox { HorizontalAlignment = HorizontalAlignment.Center };
cb.Bind(ToggleButton.IsCheckedProperty,
new Binding(nameof(ExamResultRow.Absent)) { Mode = BindingMode.TwoWay });
return cb;
}),
});
grid.Columns.Add(new DataGridTemplateColumn
{
Header = "Kommentar", Width = new DataGridLength(180, DataGridLengthUnitType.Pixel),
CellTemplate = BuildCommentCellTemplate(grid),
CellTemplate = new FuncDataTemplate<ExamResultRow>((_, _) =>
{
var tb = new TextBox
{
Name = CommentCellName,
BorderThickness = new Thickness(0),
Background = Brushes.Transparent,
Padding = new Thickness(6, 4),
};
tb.Bind(TextBox.TextProperty, new Binding(nameof(ExamResultRow.Comment))
{ Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.LostFocus });
return tb;
}),
});
}
private IDataTemplate BuildPointsCellTemplate(int taskIndex, DataGrid grid, int taskCount)
/// Die Zelle hängt sich bei jedem DataContext-Wechsel neu an die passende PointsCell, statt
/// die Zeile beim Erzeugen im Closure festzuhalten: Das DataGrid recycelt Zeilen beim
/// Scrollen, ein festgehaltener Verweis würde dann Punkte in die Zeile eines anderen
/// Schülers schreiben.
private static IDataTemplate BuildPointsCellTemplate(int taskIndex)
{
var cellName = $"Pts_{taskIndex}";
return new FuncDataTemplate<ExamResultRow>((row, _) =>
return new FuncDataTemplate<ExamResultRow>((_, _) =>
{
if (row is null) return new TextBlock();
var cell = row.Cells.ElementAtOrDefault(taskIndex);
if (cell is null) return new TextBlock();
var tb = new TextBox
{
Name = cellName,
Text = FormatPoints(cell.Value),
Name = PointsCellName,
Tag = taskIndex,
BorderThickness = new Thickness(0),
Background = Brushes.Transparent,
Padding = new Thickness(6, 4),
HorizontalContentAlignment = HorizontalAlignment.Center,
IsEnabled = cell.IsApplicable,
};
ToolTip.SetTip(tb, $"max. {FormatPoints(cell.MaxPoints)} Punkte");
UpdateInvalidVisual(tb, cell.IsInvalid);
cell.PropertyChanged += (_, pe) =>
PointsCell? cell = null;
void OnCellPropertyChanged(object? _, PropertyChangedEventArgs pe)
{
if (pe.PropertyName == nameof(PointsCell.IsInvalid))
UpdateInvalidVisual(tb, cell.IsInvalid);
if (pe.PropertyName == nameof(PointsCell.IsApplicable))
tb.IsEnabled = cell.IsApplicable;
};
if (cell is null) return;
if (pe.PropertyName == nameof(PointsCell.IsInvalid)) UpdateInvalidVisual(tb, cell.IsInvalid);
if (pe.PropertyName == nameof(PointsCell.IsApplicable)) tb.IsEnabled = cell.IsApplicable;
}
tb.LostFocus += (_, _) => CommitPointsCell(tb, cell);
tb.KeyDown += (_, e) => HandlePointsCellKeyDown(e, grid, row, taskIndex, taskCount);
tb.DataContextChanged += (_, _) =>
{
if (cell is not null) cell.PropertyChanged -= OnCellPropertyChanged;
cell = (tb.DataContext as ExamResultRow)?.Cells.ElementAtOrDefault(taskIndex);
if (cell is null)
{
tb.Text = "";
tb.IsEnabled = false;
return;
}
cell.PropertyChanged += OnCellPropertyChanged;
tb.Text = FormatPoints(cell.Value);
tb.IsEnabled = cell.IsApplicable;
ToolTip.SetTip(tb, $"max. {FormatPoints(cell.MaxPoints)} Punkte");
UpdateInvalidVisual(tb, cell.IsInvalid);
};
tb.LostFocus += (_, _) => { if (cell is not null) CommitPointsCell(tb, cell); };
return tb;
});
@@ -142,119 +188,87 @@ public partial class ExamGradingDialog : Window
private static string FormatPoints(double? value) =>
value?.ToString("0.##", CultureInfo.InvariantCulture) ?? "";
private IDataTemplate BuildAbsentCellTemplate()
/// Zwei Korrektur-Workflows (Nutzer-Feedback), umschaltbar oben im Dialog: "Schüler für
/// Schüler" (Enter läuft die Zeile entlang, am Ende weiter zur nächsten Arbeit) und "Aufgabe
/// für Aufgabe" (Enter läuft die Spalte hinunter, am Ende weiter zur nächsten Aufgabe).
/// Umschalt+Enter geht in derselben Reihenfolge zurück. Pfeiltasten bewegen sich unabhängig
/// vom Modus direkt in ihre Richtung — links/rechts nur, wenn der Cursor am Rand steht oder
/// alles markiert ist, damit man innerhalb einer Zahl noch korrigieren kann.
private void OnPreviewKeyDown(object? sender, KeyEventArgs e)
{
return new FuncDataTemplate<ExamResultRow>((row, _) =>
{
if (row is null) return new TextBlock();
var cb = new CheckBox { IsChecked = row.Absent, HorizontalAlignment = HorizontalAlignment.Center };
cb.IsCheckedChanged += (_, _) => row.Absent = cb.IsChecked == true;
return cb;
});
}
private IDataTemplate BuildCommentCellTemplate(DataGrid grid)
{
const string cellName = "Comment";
return new FuncDataTemplate<ExamResultRow>((row, _) =>
{
if (row is null) return new TextBlock();
var tb = new TextBox
{
Name = cellName,
Text = row.Comment,
BorderThickness = new Thickness(0),
Background = Brushes.Transparent,
Padding = new Thickness(6, 4),
};
tb.LostFocus += (_, _) => row.Comment = tb.Text ?? "";
tb.KeyDown += (_, e) => HandleCommentCellKeyDown(e, grid, row, cellName);
return tb;
});
}
/// Zwei Korrektur-Workflows (Nutzer-Feedback): "Arbeit für Arbeit" (ein Schüler komplett,
/// dann der nächste) und "Aufgabe für Aufgabe" (eine Aufgabe für alle Schüler, dann die
/// nächste) — beide bewusst ohne Moduswechsel über zwei verschiedene Tasten, weil sich beide
/// Reihenfolgen ohnehin schon auf die Tabelle abbilden (Zeile vs. Spalte). Enter läuft
/// zeilenweise (am Zeilenende zurück zu Aufgabe 1 der nächsten Zeile), Pfeil-hoch/-runter
/// spaltenweise (am Spaltenende weiter zur Nachbaraufgabe) — beides bricht am Tabellenende
/// einfach ab, kein Rundlauf zurück zum Anfang.
private void HandlePointsCellKeyDown(KeyEventArgs e, DataGrid grid, ExamResultRow row, int taskIndex, int taskCount)
{
if (e.Key is not (Key.Down or Key.Up or Key.Enter)) return;
e.Handled = true;
if (grid.ItemsSource is not IList items) return;
var rowIdx = items.IndexOf(row);
if (DataContext is not ExamGradingDialogViewModel vm) return;
if ((e.Source as Visual)?.FindAncestorOfType<TextBox>(includeSelf: true) is not { } tb) return;
if (tb.DataContext is not ExamResultRow row) return;
var rowIdx = vm.Rows.IndexOf(row);
if (rowIdx < 0) return;
int targetRowIdx;
int targetTaskIndex;
if (e.Key == Key.Enter)
if (tb.Name == PointsCellName && tb.Tag is int taskIdx)
{
targetTaskIndex = taskIndex + 1;
targetRowIdx = rowIdx;
if (targetTaskIndex >= taskCount) { targetTaskIndex = 0; targetRowIdx = rowIdx + 1; }
if (ToMove(e, tb) is not { } move) return;
e.Handled = true;
if (vm.NextCell(rowIdx, taskIdx, move) is { } target)
FocusPointsCell(vm, target.Row, target.Task);
}
else
else if (tb.Name == CommentCellName && e.Key is Key.Down or Key.Up or Key.Enter)
{
targetTaskIndex = taskIndex;
targetRowIdx = e.Key == Key.Up ? rowIdx - 1 : rowIdx + 1;
if (targetRowIdx < 0) { targetRowIdx = items.Count - 1; targetTaskIndex = taskIndex - 1; }
else if (targetRowIdx >= items.Count) { targetRowIdx = 0; targetTaskIndex = taskIndex + 1; }
e.Handled = true;
var targetIdx = e.Key == Key.Up || e.KeyModifiers.HasFlag(KeyModifiers.Shift) ? rowIdx - 1 : rowIdx + 1;
if (targetIdx < 0 || targetIdx >= vm.Rows.Count || Grid is not { } grid) return;
FocusCell(grid, vm.Rows[targetIdx], grid.Columns[^1],
t => t.Name == CommentCellName);
}
if (targetTaskIndex < 0 || targetTaskIndex >= taskCount
|| targetRowIdx < 0 || targetRowIdx >= items.Count) return;
FocusPointsCell(grid, items[targetRowIdx]!, targetTaskIndex);
}
/// Enter/Pfeil-Hoch/Pfeil-Runter springen im Kommentarfeld zur gleichen Spalte in der
/// Nachbarzeile (Tab funktioniert bereits über die normale Fokus-Reihenfolge).
private void HandleCommentCellKeyDown(KeyEventArgs e, DataGrid grid, object rowItem, string cellName)
private static GradingMove? ToMove(KeyEventArgs e, TextBox tb)
{
if (e.Key is not (Key.Down or Key.Up or Key.Enter)) return;
e.Handled = true;
var len = tb.Text?.Length ?? 0;
var selStart = Math.Min(tb.SelectionStart, tb.SelectionEnd);
var selEnd = Math.Max(tb.SelectionStart, tb.SelectionEnd);
var allSelected = selStart == 0 && selEnd == len;
var noSelection = selStart == selEnd;
if (grid.ItemsSource is not IList items) return;
var idx = items.IndexOf(rowItem);
if (idx < 0) return;
var targetIdx = e.Key == Key.Up ? idx - 1 : idx + 1;
if (targetIdx < 0 || targetIdx >= items.Count) return;
var targetItem = items[targetIdx]!;
grid.ScrollIntoView(targetItem, grid.CurrentColumn);
Dispatcher.UIThread.Post(() =>
return e.Key switch
{
var targetTb = grid.GetVisualDescendants().OfType<TextBox>()
.FirstOrDefault(t => t.DataContext == targetItem && t.Name == cellName);
if (targetTb is null) return;
targetTb.Focus();
targetTb.SelectAll();
}, DispatcherPriority.Loaded);
Key.Enter when e.KeyModifiers.HasFlag(KeyModifiers.Shift) => GradingMove.Previous,
Key.Enter => GradingMove.Next,
Key.Up => GradingMove.Up,
Key.Down => GradingMove.Down,
Key.Left when allSelected || (noSelection && tb.CaretIndex == 0) => GradingMove.Left,
Key.Right when allSelected || (noSelection && tb.CaretIndex == len) => GradingMove.Right,
_ => null,
};
}
/// Spalte 0 ist "Schüler", danach folgen die Aufgaben-Spalten in Reihenfolge — daher der
/// Offset von 1 beim Auflösen des Spaltenobjekts für ScrollIntoView (muss auch horizontal
/// zur Nachbaraufgabe scrollen, nicht nur vertikal zur Nachbarzeile).
private void FocusPointsCell(DataGrid grid, object targetItem, int targetTaskIndex)
/// Navigiert das DataGrid selbst (z. B. nach Klick auf den Schülernamen per Pfeiltaste) in
/// eine Aufgabenzelle, wird deren Eingabefeld direkt geöffnet, statt nur die Zelle zu markieren.
private void OnGridCurrentCellChanged(ExamGradingDialogViewModel vm, DataGrid grid)
{
var targetColumn = grid.Columns.ElementAtOrDefault(1 + targetTaskIndex);
if (targetColumn is null) return;
grid.ScrollIntoView(targetItem, targetColumn);
if (FocusManager?.GetFocusedElement() is TextBox) return;
var taskIdx = grid.CurrentColumn is { } col ? _taskColumns.IndexOf(col) : -1;
if (taskIdx < 0 || grid.SelectedItem is not ExamResultRow row) return;
var rowIdx = vm.Rows.IndexOf(row);
if (vm.IsEditable(rowIdx, taskIdx)) FocusPointsCell(vm, rowIdx, taskIdx);
}
var cellName = $"Pts_{targetTaskIndex}";
private void FocusPointsCell(ExamGradingDialogViewModel vm, int rowIdx, int taskIdx)
{
if (Grid is not { } grid || taskIdx >= _taskColumns.Count) return;
FocusCell(grid, vm.Rows[rowIdx], _taskColumns[taskIdx],
t => t.Name == PointsCellName && t.Tag is int i && i == taskIdx);
}
/// Erst scrollen (auch horizontal), dann nach dem Layout-Durchlauf das TextBox-Element der
/// Zielzelle suchen — vorher existiert es bei virtualisierten Zeilen eventuell noch nicht.
private static void FocusCell(DataGrid grid, object rowItem, DataGridColumn column, Func<TextBox, bool> match)
{
grid.ScrollIntoView(rowItem, column);
Dispatcher.UIThread.Post(() =>
{
var targetTb = grid.GetVisualDescendants().OfType<TextBox>()
.FirstOrDefault(t => t.DataContext == targetItem && t.Name == cellName);
if (targetTb is null) return;
targetTb.Focus();
targetTb.SelectAll();
var target = grid.GetVisualDescendants().OfType<TextBox>()
.FirstOrDefault(t => t.DataContext == rowItem && t.IsEffectivelyVisible && match(t));
if (target is null) return;
target.Focus(NavigationMethod.Tab);
target.SelectAll();
}, DispatcherPriority.Loaded);
}
@@ -281,7 +281,8 @@ public partial class GroupDetailView : UserControl
App.Services.GetRequiredService<IStudentRepository>(),
App.Services.GetRequiredService<IGroupMembershipRepository>(),
App.Services.GetRequiredService<GradingService>(),
exam, vm.Group.Id);
exam, vm.Group.Id,
App.Services.GetRequiredService<ExamGradingSettingsService>());
var dialog = new ExamGradingDialog { DataContext = dialogVm };
var owner = TopLevel.GetTopLevel(this) as Window;