Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What causes the <code>ScrollViewer</code> to scroll is because every time the selection of the <code>TreeView</code> is changed, a <code>ScrollIntoView</code> is called (inside the <code>TreeView</code>'s source code), </p> <pre><code>this.ItemsControlHelper.ScrollIntoView(container.HeaderElement ?? container); </code></pre> <p>and it basically tries to scroll either the header or the entire <code>TreeViewItem</code> to the very left. I first thought you could modify both <code>TreeView</code> and <code>TreeViewItem</code> styles by removing all the left <code>Margin</code> and make the header the same left margin as the expander button. However, this solution only works for first level items as in the second level, there's an indentation before the item so if you click on it it will scroll again.</p> <p>So I guess there is no pure xaml solution. The easist way to solve this issue would be, given that you've already got the custom control, instead of calling (I think there's a timing issue and that's why this code is not working)</p> <pre><code>_scrollViewer.ScrollToHorizontalOffset(0); </code></pre> <p>you do</p> <pre><code>var scrollableRegions = _scrollViewer.GetVisualDescendants().OfType&lt;IScrollInfo&gt;(); foreach (var region in scrollableRegions) { region.SetHorizontalOffset(0); } </code></pre> <p>If you really prefer xaml solutions you may as well put this code into a behavior or an attached property, and just drop it in using Blend.</p> <p>Having said all these, another solution I personally think is better and have used it in my own appliation is, disable the horizontal scrollbar entirely, and use <code>TextTrimming="WordEllipsis"</code> for all my <code>TextBlocks</code> inside <code>TreeViewItems</code> to indicate the user there's more text. Also provide a <code>GridSplitter</code> to allow the user to resize the <code>TreeView</code> content to see full text instead of scrolling back and forth horizontally. But this is just my opinion.</p> <p>Hope this helps! :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload