Skip to content

Conversation

@ace90210
Copy link
Contributor

Here is a professional PR description template formatted with Markdown, ready to be pasted into GitHub.


Fix: TreeItem Template click does not toggle Checkbox

Summary

This PR fixes an inconsistency in the RadzenTreeItem component where clicking the content of a custom Template fails to toggle the associated checkbox, whereas clicking default Text works as expected.

The Issue

When AllowCheckBoxes="true", the default rendering logic wraps the item text in a <label for="...">. This leverages native HTML behavior to toggle the checkbox when the text is clicked.

However, when a <Template> is provided, the content is wrapped in a standard <div>. As a result, clicking the template content triggers the Selection (highlight) event via event bubbling, but fails to trigger the Checked state change. This creates a confusing UX where the user expects the checkbox to toggle upon clicking the row, but it does not.

The Fix

I have updated RadzenTreeItem.razor and RadzenTreeItem.razor.cs to programmatically handle the toggle logic when a Template is clicked.

  1. Semantics Preserved: Kept the Template container as a <div> (rather than changing it to a <label>) to ensure valid HTML semantics, as custom templates may contain interactive elements (buttons, inputs) which are illegal inside a label.
  2. Event Handler: Added a protected OnTemplateClick method that checks if AllowCheckBoxes is true and toggles the CheckedChange state.
  3. Binding: Attached @onclick="OnTemplateClick" to the template wrapper div.

Technical Changes

RadzenTreeItem.razor.cs
Added handler logic:

protected async Task OnTemplateClick(MouseEventArgs args)
{
    if (Tree != null && Tree.AllowCheckBoxes && Checkable)
    {
        var currentValue = IsChecked();
        await CheckedChange(currentValue == true ? false : true);
    }
}

RadzenTreeItem.razor
Updated markup to attach the handler:

@if (Template != null)
{
    <div class=@LabelClass @onkeydown:stopPropagation @onclick="@OnTemplateClick">
        @Template(this)
    </div>
} 

Reproduction Steps

  1. Create a RadzenTree with AllowCheckBoxes="true".
  2. Define a <Template> for RadzenTreeLevel.
  3. Run the application and click the text/content inside the tree item (not the checkbox itself).
  4. Before Fix: The item is Selected (highlighted blue), but the Checkbox remains unchanged.
  5. After Fix: The item is Selected and the Checkbox toggles state.

Checklist

  • Changes tested locally.
  • Verified that Context Menus still function correctly.
  • Verified that Selection (blue highlight) still functions correctly via event bubbling.

@ace90210 ace90210 changed the title add click handler for treeviewitem when Template used with checkboxes Fix: TreeItem Template click does not toggle Checkbox Nov 28, 2025
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.

2 participants