Skip to content

Fix ListBox coercing UIElement items to string when no ItemTemplate is set#167

Merged
tedd merged 2 commits into
forge-listbox-parity-5850515012165907113from
copilot/sub-pr-148-please-work
Mar 11, 2026
Merged

Fix ListBox coercing UIElement items to string when no ItemTemplate is set#167
tedd merged 2 commits into
forge-listbox-parity-5850515012165907113from
copilot/sub-pr-148-please-work

Conversation

Copilot AI commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

ListBox.IsItemItsOwnContainerOverride only returns true for ListBoxItem, so arbitrary UIElement items (e.g. Button) are wrapped in a new ListBoxItem. Previously, PrepareContainerForItemOverride always called GetItemText(item) when ItemTemplate is null, setting the container's Content to a string and discarding the element entirely.

Changes

  • ListBox.PrepareContainerForItemOverride: adds an else if (item is UIElement) branch that assigns the element directly as Content, bypassing GetItemText. Non-UIElement items continue to fall back to GetItemText.
if (ItemTemplate != null)
{
    lbi.ContentTemplate = ItemTemplate;
    lbi.Content = item;
}
else if (item is UIElement uiElement)
{
    // Preserve UIElement items as content so they render and interact correctly
    lbi.Content = uiElement;
}
else
{
    lbi.Content = GetItemText(item);
}

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…ForItemOverride

Co-authored-by: tedd <493224+tedd@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on ListBox Parity Integration PR Fix ListBox coercing UIElement items to string when no ItemTemplate is set Mar 11, 2026
Copilot finished work on behalf of tedd March 11, 2026 13:09
@tedd
tedd marked this pull request as ready for review March 11, 2026 13:17
Copilot AI review requested due to automatic review settings March 11, 2026 13:17
@tedd
tedd merged commit de772a8 into forge-listbox-parity-5850515012165907113 Mar 11, 2026
3 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes ListBox item hosting so that UIElement items (e.g., Button, TextBlock) are not coerced into strings when ItemTemplate is unset, preserving their rendering and interaction behavior within the ListBoxItem container model used for selection.

Changes:

  • Update ListBox.PrepareContainerForItemOverride to detect UIElement items and assign them directly to ListBoxItem.Content when ItemTemplate is null.
  • Keep existing behavior for non-UIElement items by continuing to fall back to GetItemText(item).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Tedd.TUI/ListBox.cs
Comment on lines +111 to +115
else if (item is UIElement uiElement)
{
// Preserve UIElement items as content so they render and interact correctly
lbi.Content = uiElement;
}

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit coverage for the new UIElement-item branch (e.g., a TextBlock or Button in Items with ItemTemplate == null). Without a test, it’s easy to regress back to coercing UIElement items via GetItemText, since the existing ListBox render tests only cover string items and templated items.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants