Skip to content

MudTreeView: Fix multi-selection checkboxes for items without a value - #13951

Merged
danielchalmers merged 1 commit into
MudBlazor:devfrom
AtakanUk:fix/treeview-multiselection-leaf-indeterminate
Oct 7, 2026
Merged

danielchalmers merged 1 commit into
MudBlazor:devfrom
AtakanUk:fix/treeview-multiselection-leaf-indeterminate

Conversation

@AtakanUk

@AtakanUk AtakanUk commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #13233

In SelectionMode.MultiSelection, items without a Value broke the checkboxes:

  • A leaf without a value (the snippet in the issue, where the ItemTemplate doesn't pass Value) showed as partially checked after the first click and as checked after the second, while nothing was selected and SelectedValues stayed empty.
  • A parent without a value showed as partially checked when all of its children were selected, and clicking it again couldn't clear them.
  • A parent with a value but a child without one (for example a note row) behaved the same way: partially checked when selected, and impossible to uncheck.

Cause:
Selection is tracked by value, so an item without a value can never be selected, but two places still counted such items:

  • MudTreeViewItem.ComputeCheckBoxStateTriState counted them as unselected, so their parent could never show as fully checked. A parent without a value also counted itself.
  • MudTreeView.OnItemClickAsync included them when deciding whether everything under the clicked item was already selected. They never are, so the click selected everything again instead of clearing it.

A click on a leaf without a value changes nothing in the tree, so the item doesn't re-render. The tri-state MudCheckBox has already moved to its next state by itself (unchecked → partially checked → checked), and re-rendering with the same value wouldn't reset it either, because the checkbox only takes a parameter value when it changes.

Change:

  • Items without a value are left out of the tri-state calculation and out of the "all selected" check. A parent without a value now shows the state of its children, and clicking any parent selects or clears what can be selected under it. This matches AutoSelectParent, which already ignores children without a value.
  • The checkbox of a leaf without a value is read-only, since there is nothing to select. Clicks and the keyboard no longer change it. It is read-only rather than hidden or disabled, so the layout stays the same and the checkbox isn't greyed out; it only loses its hover effect.

Items still need a Value (or Text for string) to be selectable. Trees where every item has one behave as before.

Not changed: a parent whose sub-items have no values either is not made read-only. Its children are only known after it renders, so it would need extra state tracking for an edge case where nothing can be selected anyway.

No public API changes.

Before/After:
Three multi-selection trees: the snippet from the issue, a folder without a value, and a parent with a note row that has no value. Each checkbox is clicked twice.

Before:

treeview_novalue_before.mp4

After:

treeview_novalue_after.mp4

Testing:

  • Five tests (seven cases) were added to TreeViewTests, using the new viewer component TreeViewMultiSelectionNoValueTest:

    • TreeViewWith_MultiSelection_LeafWithoutValue_ShouldNotBeCheckable
    • TreeViewWith_MultiSelection_ParentWithoutValue_ShouldToggleAndReflectItsChildren
    • TreeViewWith_MultiSelection_ChildWithoutValue_ShouldNotBlockParentToggle
    • TreeViewWith_MultiSelection_AutoSelectParent_ShouldCheckParentWithChildWithoutValue
    • TreeViewWith_MultiSelection_InitialSelection_ShouldIgnoreItemsWithoutValue

    All of them fail without the change, and each of the three code changes is needed for at least one of them to pass.

  • In the browser (Edge), on the snippet from the issue, mouse clicks and Space/Enter/Backspace no longer change the checkbox of a leaf without a value. Before, they moved it between partially checked and checked.

  • On the TreeView docs page (MudBlazor.Docs.Server), clicking every checkbox in the multi-selection examples gives the same results before and after the change.

  • A randomized check compared every rendered checkbox and SelectedValues against the expected result after each click. It covered 1,200 generated trees with 35% or 65% of the items without a value, with and without AutoSelectParent, and 18,000 clicks in total, and found no differences. On trees where every item has a value, the rendered states after 3,750 clicks were identical before and after the change.

  • On top of the latest dev, in Release: MudBlazor.UnitTests 6151 passed (2 skipped), MudBlazor.UnitTests.Docs 1420 passed, MudBlazor.UnitTests.Analyzers 391 passed, and dotnet format --verify-no-changes is clean. The library builds for net8.0, net9.0 and net10.0 without warnings.

AI assistance was used for the tests; all changes were reviewed and verified locally.

Checklist:

  • I've read the contribution guidelines
  • My code follows the style of this project
  • I've added or updated relevant unit tests

Selection is tracked by value, so an item without a value can never be selected, but the multi-selection checkboxes still counted such items. A parent with a child without a value, or a parent without a value of its own, showed as partially checked when everything selectable under it was selected, and clicking it again could not clear the selection.

Leave items without a value out of the tri-state calculation and out of the check that decides whether a click selects or clears. A leaf without a value has nothing to select, so its checkbox is now read-only instead of moving through partially checked and checked on its own.

Fixes MudBlazor#13233
@mudbot mudbot Bot added the bug Unexpected behavior or functionality not working as intended label Oct 6, 2026
@danielchalmers

Copy link
Copy Markdown
Member

Thanks!

@danielchalmers
danielchalmers merged commit 1b18e00 into MudBlazor:dev Oct 7, 2026
12 checks passed
@AtakanUk
AtakanUk deleted the fix/treeview-multiselection-leaf-indeterminate branch October 7, 2026 18:12
This was referenced Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MudTreeView: leaf items are shown as partially selected in MultiSelection mode

2 participants