Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrease indentation in TreeViewItem for deepest node

Tags:

wpf

xaml

How can I decrease the left indentation of the deepest TreeViewItem node? For some reason WPF seem to increase this indentation by quite a bit for all but the deepest node. Looking at Window XP's tree view implementation each depth has the same left margin.

I've searched around and I can't seem to find an implementation that works yet it appears to be possible.

like image 366
gcso Avatar asked Feb 03 '26 14:02

gcso


1 Answers

UPDATE: Corrected the solution

Here is a picture of the problem for reference...

enter image description here

And here is a picture of what you're (probably) hoping for...

enter image description here

NOTE: The list of files appears further to the left in the second screenshot.

After digging into this, I consider it a defect in the default TreeViewItem template. To fix this and to make any other adjustments needed, you'll need to provide your own TreeViewItem template. Starting with the default TreeViewItem template from this SO answer...

You need to change this...

<Trigger Property="HasItems" Value="false">
    <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
</Trigger>

To this...

<Trigger Property="HasItems" Value="false">
    <Setter Property="Visibility" TargetName="Expander" Value="Collapsed"/>
</Trigger>

That should do it for you. By default, the system is leaving the 19 pixels of space for the exapander. Since the expander isn't needed at the leaf level, that area should be collapsed, not hidden.

like image 148
John Laffoon Avatar answered Feb 06 '26 11:02

John Laffoon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!