✅ WPF trees with some items that can't be selected
I'm building a UI with a tree view where some items exist only to group children. The tree will function as a kind of vertical tree control. When most items in the tree are selected, I'll show a control next to the tree. But the items that are only groups don't have a control to show. So I don't want to allow their selection.
How do I pull that off?
47 Replies
I've done this before, but it was years ago, let me see if I can find th repo
framework?
Well, I'm using .NET 7 if that's what you mean.
Oh I sorta assumed wpf
Well, the topic does say WPF. I thought you meant which version of .NET. That's implied when you talk about WPF.
So this is really dirty
Dirty????
maybe rough is a better term
no mvvm, pretty much wpf as winforms
but the real concept is the hierarcal data template
and the data template selector
if I understand the problem your asking anyway
Until your sample arrived, I thought you meant what I wanted was dirty.
no, I mean my code is ugly
now that I'm re-reading you may be asking for something slightly different
Uh, what did you think I wanted?
displaying diffrent classes of nodes according to types ( or some other discriminator )
Nope. There are some nodes that the user can't select. In Visual Studio's preferences window, there are tree elements that select the first child. In my case, it's similar, but I'd prefer to ignore the user's attempts to select some elements.
oh, so if they select a node, based on some sieve you'd essentially ignore it?
they can't select it at all?
Correct, or the tree ignores the click.
hmmmm
probably the easiest is to bind a command to the node
and ignore the command if it doesn't fit the criteria
I've not done wpf for a long time though
You use something newer?
nah, it was always a hobby framework for me
and I got less interested in wpf
Using the selected template you can get creative around which items are interactable
so for instrance, I have an unused property called "HasChildren"
which could be part of your template selector
so for a container template it's display only
and for other template types it's more interactive
so the concept is similar,
Hmm. I was going with one template as all entries in the source are wrapped in what I'm calling
ViewTreeData
instances.
Though calling that property CanBeSelected
is more appropriate. ViewTreeData
has that now.
My lone DataTemplate generates a specialized ViewTreeItem
.I used to play something called legend of the five rings, which is a d10 pen and paper d&d game, but has pretty complex die rolling scenarios, so I grouped "scenarios" by containers in a tree
And using the selector I could pick the appropriate "viewer"
I think for containers my view was just editing the container name
@Mayor McCheese Basically, I wasn't planning on the tree items being much different other than if you can select them. Each would be a icon with text. I might end up causing the text color and/or style to change, but the icon for entries you can't select will always be special.
Are you going the mvvm route?
Sort of. The
ViewTreeData
is serving as the View Model.Can you show your xaml?
Not at the moment. Wrong computer.
Np
I'll take a look again a bit later today
I'm on crisis schedule today
my bad, I someow missed "WPF" in the title
so, like
you just want certain tree nodes to not be selectable?
Correct.
what the trouble you're having with that?
Huh? Having trouble? I don't know how to prevent selection.
is there not a styling property you can set?
You mean a property in the tree? No. Ditto for a
TreeItem
.Focusable="false"
?I'm at the gym; but remember when I said dirty?
Naming controls is usually not needed in wpf
It's kinda frowned on mostly
But at a glance I like @V.EINA Jaken's focusable; but I'd use a template selector too to pick the data template; I don't like really long templates.
definitely
Why do I need more templates?
because you have different types of items that you want to display differently
you'll setup a
HierarchalDataTemplate
for the group items that have children, and you want to be not-selectable
and you'll have a regular DataTemplate
for the restNo. I don't. All items are displayed the same. Some are selectable. Some aren't. Some items with children are selectable.
what differentiates items from being selectable or not?
They exist for grouping ONLY. The tree functions a little like a tab control. Only some items in the tree don't correspond to anything to be displayed next to the tree.
yes, what differentiates those items?
I declared a
CanBeSelected
property. If it's true, the item is a group, but no more. There's nothing to display.bind that to
Focusable
For one, extending logic into xaml is a bad practice, it's difficult to debug and separates out concerns in unexpected ways
But if it's always the one deal then sure just bind the property
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.Thanks anyway. But after a few days thinking about it, the groups that can be selected no longer made sense. So I removed them.