Can't set IsSelected to true when dynamically loading TreeListBoxItem

Grids for WPF Forum

Posted 2 months ago by david lin
Version: 24.1.1
Platform: .NET 6.0
Environment: Windows 11 (64-bit)
Avatar

Hello, Actipro developers. When I use TreeListBox to dynamically load data, I find that when I set IsSelected in ViewModel to true, the selected highlight style does not take effect.

After my inspection, my ViewModel is correct before it is passed to the control, but when the control accepts ViewModel to create a "TreeNode" object (you seem to use "TreeNode" as an intermediate layer, my guess), the "IsSelected" of the returned "TreeNode" is false, which seems to notify my ViewModel data back, causing the failure of setting IsSelected to true.

This is the problem I found during debugging. After creating the "TreeNode" object, it will be stored in a "map". The "IsSelected" returned here is not the correct data in my ViewModel, but the "Item" passed into the constructor is correct.

I don't know if my inference is correct, here I give my simplest case to support you in checking this issue.

Simplest Case Address

Comments (4)

Posted 2 months ago by david lin
Avatar

If possible, please at least tell me how to deal with this problem, thanks!

Answer - Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

This took a while to track down since when debugging the issue, things weren't making sense for logic paths in our code.  We eventually narrowed it down to part of the problem being the NotifyPropertyChanged implementation in your sample.  You accidentally raise the PropertyChanged event twice on property changes, once before and once after the actual field is updated.  You need to remove the event raise before the field is updated.

Once that is done, also add this to your TreeNodeItemAdapter class.  These properties tell our internal code to watch for changes to those properties from your view models:

public TreeNodeItemAdapter() {
	this.IsExpandedPath = "IsExpanded";
	this.IsSelectablePath = "IsSelectable";
	this.IsSelectedPath = "IsSelected";
}

Then the final update needed is that as you dynamically load children, setting them IsSelected = true initially won't trigger the selection update.  You can set IsSelected = true in a line right after the new dynamic node is added to the node tree though and that will work.

Those three changes should get it going how you would like it.


Actipro Software Support

Posted 2 months ago by david lin
Avatar

Successfully processed!

But the decisive factor is the third step: add the whole tree and then set "IsSelected". The first two steps are my mistakes, but they are not related to this problem.

This seems to avoid the problem. I still can't understand why setting its properties when constructing a child item will fail.

The problem still exists. Will you fix it?

In fact, we users have more ways to solve this problem, but the concern is that this will make us write more code that is not related to the business, which is not cool.

Finally, Thanks again.

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Something to keep in mind is how the TreeListBox is virtualized and does not have direct access to the view models you create.  We can't walk the entire tree of items being added to look for selected items, since in many cases such as with lazy-loading, that could be very time consuming.

That being said, we can look at the root items being added and can call into the item adapter to see if they are selected.  If those items are expanded, we can continue walking down the tree through already-expanded nodes to look for selected ones.  That code update will be in the next release.


Actipro Software Support

The latest build of this product (v24.1.5) was released 4 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.