Is it possible to disable/change the default selection logic of CheckBoxTree?
31 次查看(过去 30 天)
显示 更早的评论
When checking/unchecking nodes in a CheckBoxTree, the following rules are applied (from CheckBoxTree Properties documentation):
- If CheckedNodes contains a parent node, all the children of the parent node are automatically added to CheckedNodes.
- If CheckedNodes contains all the children of a parent node, the parent node is automatically added to CheckedNodes.
While I appreciate the first rule, the second one makes this UI element unusable for me.
I would like to know if there is a way to either change these rules (e.g. disable rule 2) or disable them so I can implement my own in CheckedNodesChangedFcn callback. When modifying the CheckedNodes property of CheckBoxTree programmatically, these rules are applied as well, so just modifying that list is not a workaround unfortunately.
0 个评论
回答(2 个)
Mario Malic
2023-11-3
编辑:Mario Malic
2023-11-3
As it is now, it is impossible. Parent nodes will always be selected if all child nodes are. I have made a callback function and this is silly that it's impossible to do it. If it's helpful, you can use normal uitree use multiselect option, as this is the one that doesn't trigger the parent selection.
function treeCheckedNodesChangedFcn(src, evt)
if ~isempty(src.CheckedNodes)
% Find all nodes
allNodes = findall(src, "Type", "uitreenode");
% Find parent indices to remove
parentCheckedNodes = evt.ParentCheckedNodes;
parentIdx = ismember(allNodes, parentCheckedNodes);
% Find children indices and remove parent indices
selectedCheckedNodes = evt.CheckedNodes;
selectedChildrenNodesIdx = ismember(selectedCheckedNodes, parentCheckedNodes);
childrenNodes = selectedCheckedNodes(~selectedChildrenNodesIdx);
nodeChildrenIdx = ismember(allNodes, childrenNodes);
src.CheckedNodes = allNodes(nodeChildrenIdx);
end
end
Maybe this would be interesting for you @Adam Danz
1 个评论
Adam Danz
2023-11-3
编辑:Adam Danz
2023-11-3
As @Mario Malic mentioned, it's not possible to replace that logic. I'm curious what your use case is as to why the value of the parent node should not indicate the collective state of its children nodes. We may be able to come up with a more targetted workaround if we understand how you're using the checkbox tree and why the value of the parent should be independent to the values of its children.
It's fairly easy to detect when a parent was auto selected or auto deselected. In this demo below, any time a parent's value is changed automatically, a statment is printed to the MATLAB command window indicating the parent name and whether it was auto selected or auto deselected. The CheckedNodesChangedFcn would have to be appended if you wanted to store those values between calls.
%% Create checkbox tree
fig = uifigure;
t = uitree(fig,'Checkbox',"Position",[20 20 150 150]);
t.CheckedNodesChangedFcn = @treeCheckedNodesChangedFcn;
% First level nodes
category1 = uitreenode(t,"Text","Runners","NodeData",[]);
category2 = uitreenode(t,"Text","Cyclists","NodeData",[]);
% Second level nodes.
% Node data is age (y), height (m), weight (kg)
p1 = uitreenode(category1,"Text","Joe","NodeData",[40 1.67 58] );
p2 = uitreenode(category1,"Text","Linda","NodeData",[49 1.83 90]);
p3 = uitreenode(category2,"Text","Rajeev","NodeData",[25 1.47 53]);
p4 = uitreenode(category2,"Text","Anne","NodeData",[88 1.92 100]);
% Expand the tree
expand(t);
function treeCheckedNodesChangedFcn(src, evt)
% Display a message in the command window when a parent is automatically
% selected or de-deselected.
selectedNode = src.SelectedNodes;
parentsChecked = evt.ParentCheckedNodes;
prevParentsChecked = evt.PreviousParentCheckedNodes;
% Auto-selected detection
% If a parent node is in the list of currently selected parents but not in
% the list of previously selected parents, and if the parent node wasn't
% just manually selected by the user, print a statement to the command
% window.
isAutoSelected = ~ismember(parentsChecked, selectedNode) & ~ismember(parentsChecked, prevParentsChecked);
if any(isAutoSelected)
nodeText = get(parentsChecked(isAutoSelected),'Text');
fprintf('%s was auto selected.\n',nodeText)
end
% Auto de-selection detection
% If a parent node is not in the list of currently selected parents but is in
% the list of previously selected parents, and if the parent node wasn't
% just manually de-selected by the user, print a statement to the command
% window.
isAutoDeselected = ~ismember(prevParentsChecked, selectedNode) & ~ismember(prevParentsChecked, parentsChecked);
if any(isAutoDeselected)
nodeText = get(prevParentsChecked(isAutoDeselected),'Text');
fprintf('%s was auto deselected.\n',nodeText)
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!