·4 min read
TaskPad comment editing threw because WPF panels do not allow Children[i] = x
The edit button on a TaskPad comment did one job: swap the message text for a text box. One line did the swap, and it was a line WPF refuses to run. The commit that fixed it is titled "make comment editing work", which is the polite way of saying the feature I had shipped did not.
- Project
- TaskPad
- Date
- Oct 2026
- Stack
- C#, WPF
- Commit
- 25a3d6f
The shape of a comment card
TaskPad lets you attach comments to a line of a task. Each comment is rendered as a small card in code, in src/Comments.cs: a header with author and date, a row of tool icons that dims until you hover, and the message body. The pieces sit in a StackPanel called holder:
var holder = new StackPanel { Margin = new Thickness(0, index == 0 ? 0 : 8, 0, 0), Background = Brushes.Transparent };
holder.Children.Add(head);
holder.Children.Add(body);body is a TextBlock. Editing means replacing it with a TextBox filled with the same text, focusing the box with the caret at the end, and putting the TextBlock back when you press Enter or leave. Double-clicking the message, or clicking the pencil, starts the edit.
The line that could not work
The original swap was one expression:
holder.Children[holder.Children.IndexOf(body)] = box;It reads naturally, since Children has an indexer and a list-like feel. The comment I left next to the replacement says what the framework does about it: a panel slot cannot be overwritten in place, WPF throws. UIElementCollection supports Add, Insert and RemoveAt, but assigning to an index is not something it lets you do. The setter exists because the type implements the list interface, and it throws at runtime rather than being a compile error. Nothing at build time warns you.
So the edit path threw instead of showing the box. From the user's side that is simply editing not working.
The fix
Take the text block out, then put the box in at the same index:
// a panel slot can't be overwritten in place (WPF throws): take the text out, then put the box in
int at = holder.Children.IndexOf(body);
holder.Children.RemoveAt(at);
holder.Children.Insert(at, box);Capturing the index first matters. The header is at index 0 and the body at index 1, and the box has to land exactly where the text was, otherwise the layout jumps. Because the guard at the top of StartEdit checks holder.Children.Contains(body) and returns early, a second trigger while the box is already showing does nothing instead of throwing a second time.
The code looked fine and compiled fine, so reading it would not have caught this. Running the edit path would have.
Cleaning up while the file was open
The same commit reorganised the tool row, because adding copy exposed some duplication. Delete had been an inline lambda on the delete icon. A right-click menu needed the same action, so it became a local function used by both:
void Delete()
{
Comments.Update(_ed.Document, Start, t => { t.RemoveAt(index); return t; });
if (Start < 0 || !Comments.Find(_ed.Document, Start, out _, out _)) Done?.Invoke(); else Build();
}The card now has a copy icon, a pencil and a cross, and a context menu with Copy, Edit and Delete wired to the same three functions. The commented-text quote row got its own menu with Copy and "Edit commented text".
Copy gave feedback a small design question. Clipboard calls can fail, for instance when another process holds the clipboard, so the helper swallows that failure and does not claim success. On success it briefly swaps the icon for a check mark, then restores it:
static void Copy(string text, Border feedback)
{
try { Clipboard.SetText(text); } catch { return; }
if (!(feedback?.Child is TextBlock tb)) return;
var old = tb.Text;
tb.Text = "✓";
var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1200) };
timer.Tick += (s, e) => { timer.Stop(); tb.Text = old; };
timer.Start();
}Passing null as the feedback target is how the context menu reuses the function: the menu has no icon to flash, so it copies and nothing else.
What I would do differently
Two things. First, the failing line was short and plausible, and I trusted it because it looked like a list. An API that looks like a collection but throws on parts of the collection contract is worth remembering: for WPF panels, treat Children as add, insert and remove only.
Second, a feature that is only exercised by its happy path in my head is not finished. The edit path needed one click to fail. The follow-up commit is short and boring, and that is the right size for this kind of mistake. The cost was a feature that looked finished and was not.
#wpf#csharp#desktop#bugfix#ui
Keep reading
Have a project in mind?