Back to Devexpress

Checkboxes in Tree View for .NET MAUI

maui-404903-tree-view-node-checkboxes.md

latest7.1 KB
Original Source

Checkboxes in Tree View for .NET MAUI

  • Sep 18, 2024
  • 3 minutes to read

You can use checkboxes to select one or multiple nodes. The following code snippet displays and configures node checkboxes within the DXTreeView control:

xaml
<dx:DXTreeView ...
    ShowCheckBoxes="True"
    CheckBoxPosition="Inline"
    CheckActionTarget="Node"
    CheckBoxRecursiveChecking="True"
    HighlightCheckedNodes="False"
    CheckBoxExpandAnimationDuration="0:0:2"
    CheckBoxExpandAnimationMode="FromCenter"
    CheckBoxFieldName="IsChecked">
    ...
</dx:DXTreeView>

Use the following API to configure checkboxes:

ShowCheckBoxesGets or sets whether to display node check boxes. This is a bindable property.CheckBoxPositionGets or sets the checkbox position. This is a bindable property.CheckActionTargetGets or sets which elements can trigger a check state change. This is a bindable property.CheckBoxRecursiveCheckingGets or sets whether to check nodes recursively. This is a bindable property.HighlightCheckedNodesGets or sets whether to highlight checked nodes. This is a bindable property.CheckBoxExpandAnimationDurationGets or sets the duration of the animation that shows/hides node checkboxes. This is a bindable property.CheckBoxExpandAnimationModeGets or sets the animation effect that shows/hides node checkboxes. This is a bindable property.CheckBoxFieldNameGets or sets the name of the data object’s property to which the tree node checkbox is bound. This is a bindable property.CheckedItemsGets or sets the collection of checked nodes. This is a bindable property.CheckBoxValueConverterGets or sets a converter that specifies a tree node checkbox value. This is a bindable property.

Checkbox Selection

Tree View checkboxes support Independent and Recursive selection.

Recursive Selection

If you want to select checkboxes recursively, enable the CheckBoxRecursiveChecking property:

  • When you check/uncheck a parent node, its children become checked/unchecked.

  • When you check/uncheck all child nodes, its parent becomes checked/unchecked.

  • When you check/uncheck a child node, but not all children are checked/unchecked, the parent node goes to the indeterminate state.

The following code snippet enables recursive checkbox selection:

xaml
<dx:DXTreeView ...
    CheckBoxRecursiveChecking="True">
    ...
</dx:DXTreeView>

Independent Selection

To select each checkbox independently, set the CheckBoxRecursiveChecking property to False.

The following code snippet enables independent checkbox selection:

xaml
<dx:DXTreeView ...
    CheckBoxRecursiveChecking="False">
    ...
</dx:DXTreeView>

Highlight Checked Nodes

If you want to highlight checked nodes, set the HighlightCheckedNodes property to true.

The following code snippet highlights checked nodes:

xaml
<dx:DXTreeView ...
    HighlightCheckedNodes="True">
    ...
</dx:DXTreeView>

Show and Hide Checkboxes

You can show checkboxes with animation effects when the ShowCheckBoxes property is set to true an runtime.

To enable the animation, set the CheckBoxExpandAnimationMode property to one of the following values:

  • FromStartToEnd — the animation effect reveals the element from start to end.
  • FromEndToStart — the animation effect reveals the element from end to start.
  • FromCenter — the animation effect reveals the element from center.
  • None — shows/hides checkboxes without animation effects.

Use the CheckBoxExpandAnimationDuration property to specify the animation duration for checkbox visibility changes.

xaml
<dx:DXTreeView ...
    CheckBoxExpandAnimationDuration="0:0:2"
    CheckBoxExpandAnimationMode="FromCenter">
    ...
</dx:DXTreeView>

Check and Uncheck Nodes at Runtime

The DXTreeView exposes an API that allows you to check/uncheck nodes at runtime:

CheckAllNodes()Checks all nodes.UncheckAllNodes()Unchecks all nodes.IsCheckedGets or sets whether the node is checked. This is a bindable property.

Respond to User Actions when Checking and Unchecking Checkboxes

The CheckBoxStateChanged event allows you to perform a custom action after a user changed the checkbox state.

xaml
<dx:DXTreeView ...
    ShowCheckBoxes="True"
    CheckBoxStateChanged="OnTreeNodeCheckBoxStateChanged">
    ...
</dx:DXTreeView>
csharp
void OnTreeNodeCheckBoxStateChanged(object sender, TreeNodeCheckBoxStateChangedEventArgs e) {
    var vm = (FirstLookPageViewModel)BindingContext;
    if (e.OldState == true)
        vm.RemoveCheckedNode((ReportLibraryNode)e.Node.Item);
    if(e.NewState == true)
        vm.AddCheckedNode((ReportLibraryNode)e.Node.Item);
}

Change Checkbox Appearance

The DXTreeView control allows you to change checkbox appearance. For more information, refer to the following help topic: Change Checkbox Appearance.