views:

29

answers:

2

Hi everyone

I have a TreeView with a given width. When I add a node with a name which makes it exceed the maximal TreeView width it displays a vertical scrollbar.

I'd prefer to display only the beginning of the name followed by '...' to avoid the scrollbar. I tried googleing it but I fail to find a result.

Could someone tell me how it's done (Not the usage of Google, I mean the string shortening)?

Cheers AC

+1  A: 

Typically like this:

<TextBlock Text="Whatever" TextTrimming="CharacterEllipsis"/>

However, you will need to incorporate this into your TreeViewItems by way of a template.

HTH,
Kent

Kent Boogaart
That's what I was looking for. In order for this to work I have to set the MaxWith-property of the TextBlock, right? How can I get the current width of the TreeView?
Anonymous Coward
Setting a width on your TreeView won't prevent scrolling - it will just change the threshold at which it starts to scroll. To disable scrolling, set `ScrollBar.HorizontalScrollBarVisibility="Disabled"` on your `TreeView`.
Kent Boogaart
@Anonymous why so? Just use the WordEllipsis as Kent suggessted, that should work.
Avatar
Thanks. I had to use ScrollViewer instead of ScrollBar but it works now.
Anonymous Coward
Sorry, yep - ScrollViewer what I meant to write :)
Kent Boogaart
A: 

first you can set a MaxWidth on your treeview to avoid this vertical scrolling stuff. second, if you are actually adding a Tree item to your tree you can set the text, than register to the loaded event of the control, check his actual size and do the text modification.

but the most elegant thing you can do in my opinion is work with data binding and hie hierarchical data template and a converter that will trim the text for you(you can send the tree item to the converter)

hope this will help you

Chen Kinnrot