tags:

views:

2339

answers:

3

Is there a way to select manually a node in virtualizing TreeView and then bring it into view?

The data model I'm using with my TreeView is implemented based on the VM-M-V model. Each TreeViewItem's IsSelected property is binded to a corresponing property in ViewModel. I've also created a listener for TreeView's ItemSelected event where I call BringIntoView() for the selected TreeViewItem.

The problem with this approach seems to be that the ItemSelected event won't be raised until the actual TreeViewItem is created. So with the virtualization enabled node selection won't do anything until the TreeView is scrolled enough and then it jumps "magically" to the selected node when the event is finally raised.

I'd really like to use virtualization because I have thousands of nodes in my tree and I've already seen quite impressive performance improvements when the virtualization has been enabled.

+1  A: 

Here is an example taken from an MSDN Question public void ScrollToItem(int index)

    {

        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background,

            (System.Windows.Threading.DispatcherOperationCallback)delegate(object arg)

            {

                int N = fileList.Items.Count;

                if (N == 0)

                    return null;

                if (index < 0)

                {

                    fileList.ScrollIntoView(fileList.Items[0]); // scroll to first

                }

                else

                {

                    if (index < N)

                    {

                        fileList.ScrollIntoView(fileList.Items[index]); // scroll to item

                    }

                    else

                    {

                        fileList.ScrollIntoView(fileList.Items[N - 1]); // scroll to last

                    }

                }

                return null;

            }, null);

    }
Micah
A: 

Micah - you're answer works only for a ListBox derived item. The posted question asks specifically about the TreeView, which inherits from ItemsControl. Neither TreeView nor TreeViewItem support the ScrollIntoView method described in your post.

Any other takers?

Mark
A: 

I used an attached property to solve this issue.

public class TreeViewItemBehaviour
{
    #region IsBroughtIntoViewWhenSelected

    public static bool GetIsBroughtIntoViewWhenSelected(TreeViewItem treeViewItem)
    {
        return (bool)treeViewItem.GetValue(IsBroughtIntoViewWhenSelectedProperty);
    }

    public static void SetIsBroughtIntoViewWhenSelected(
      TreeViewItem treeViewItem, bool value)
    {
        treeViewItem.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
    }

    public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
        DependencyProperty.RegisterAttached(
        "IsBroughtIntoViewWhenSelected",
        typeof(bool),
        typeof(TreeViewItemBehaviour),
        new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));

    static void OnIsBroughtIntoViewWhenSelectedChanged(
      DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TreeViewItem item = depObj as TreeViewItem;
        if (item == null)
            return;

        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
        {
            item.Loaded += item_Loaded;
        }
        else
        {
            item.Loaded -= item_Loaded;
        }
    }

    static void item_Loaded(object sender, RoutedEventArgs e)
    {
        TreeViewItem item = e.OriginalSource as TreeViewItem;
        if (item != null)
            item.BringIntoView();
    }

    #endregion // IsBroughtIntoViewWhenSelected

}

And in my XAML style for a TreeViewItem, I just set the property to true

<Setter Property="Behaviours:TreeViewItemBehaviour.IsBroughtIntoViewWhenSelected" Value="True" />

HTH