views:

379

answers:

2

I have a sorted tree view with a lot of item. When I am adding a new item I would like the newly added item to be selected and shown on the screen. For example: if I am seeing the first 10 items on the screen and the new item is added on 20th position then the view should change to present the new added item (it can be the first one on the screen, the last one or in the middle - it doesn't matter). I am able to get selection working after reading some blog posts. Please help me to find out how to achieve the desired functionality concerning showing on the screen.

Lukasz Glaz

A: 

If you already got the TreeViewItem corresponding to your newly added item, all you have to do is to call yourItem.BringIntoView().

Julien Lebosquain
How to deal with it when I am using mvvm pattern and I do not directly deal with TreeViewItems?
GUZ
+2  A: 

I suggestion you take a look at this article where the author show how to use an attached behavior. With this behavior, you can do everything in XAML:

 <TreeView.ItemContainerStyle>
  <Style TargetType="{x:Type TreeViewItem}">
    <Setter 
      Property="local:TreeViewItemBehavior.IsBroughtIntoViewWhenSelected" 
      Value="True" 
      />
    </Setter>
   </Style>
  </TreeView.ItemContainerStyle>

Where IsBroughtIntoViewWhenSelected is an attached property.

Jalfp
Thank you for the solution. It works great when virtualization is switched off. When virtualization is switched on and I add a couple of items, scroll a bit up and down then the application seems to freeze in some kind of loop: the currently selected item moves up and down and it is not possible to stop it.
GUZ
I have found the solution to the problem with virtualization in the following place:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/31ea27c0-32bf-4fae-a806-204f06c198b8
GUZ