0

I need to scroll to the selected item in TreeView in WPF.

XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ChartsDisplayer2016.Core.DataSelector.Views"
        xmlns:localModels="clr-namespace:ChartsDisplayer2016.Core.DataSelector.Models"
        xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="ChartsDisplayer2016.Core.DataSelector.Views.DataSelectorUniversalView"
        Title="DataSelector" Height="700" Width="585.508" ResizeMode="CanResizeWithGrip">


            <TreeView ItemsSource="{Binding FirstGeneration}" Margin="1,1,1,0">
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                        <Setter Property="FontWeight" Value="Normal" />
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="FontWeight" Value="Bold" />
                                <Setter Property="localModels:TreeViewItemBehavior.IsBroughtIntoViewWhenSelected" Value="True" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>

                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                        <TextBlock Text="{Binding Name}" />
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>     
</Window>

TreeViewItemBehavior:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;


namespace ChartsDisplayer2016.Core.DataSelector.Models
{
    public static class TreeViewItemBehavior
    {
        #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(TreeViewItemBehavior),
            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.Selected += OnTreeViewItemSelected;
            else
                item.Selected -= OnTreeViewItemSelected;
        }

        static void OnTreeViewItemSelected(object sender, RoutedEventArgs e)
        {
            // Only react to the Selected event raised by the TreeViewItem
            // whose IsSelected property was modified. Ignore all ancestors
            // who are merely reporting that a descendant's Selected fired.
            if (!Object.ReferenceEquals(sender, e.OriginalSource))
                return;

            TreeViewItem item = e.OriginalSource as TreeViewItem;
            if (item != null)
                item.BringIntoView();
        }

        #endregion // IsBroughtIntoViewWhenSelected
    }
}

The error appears in line <Setter Property = "localModels:TreeViewItemBehavior.IsBroughtIntoViewWhenSelected" Value="True" />:

The name "TreeViewItemBehavior" does not exist in the namespace "clr-namespace:ChartsDisplayer2016.Core.DataSelector.Models"

I have tried rebuilding and restarting Visual Studio as suggested here, but it didn't help.

What can I do to get rid of this error?

Community
  • 1
  • 1
Jaroslaw Matlak
  • 574
  • 1
  • 12
  • 23
  • Is the TreeViewItemBehavior class defined in the same project as the window? Otherwise you should specify the name of assembly where it is defined in your XAML namespace mapping: xmlns:localModels="clr-namespace:ChartsDisplayer2016.Core.DataSelector.Models;assembly=ModelAssemblyName". – mm8 Jan 11 '17 at 11:22
  • @MANIKANDANNAGALAKSHMI - it's not working. – Jaroslaw Matlak Jan 11 '17 at 11:27
  • @mm8 - Yes, it is defined in the same project. – Jaroslaw Matlak Jan 11 '17 at 11:27
  • Then it should work to build the Project. Make sure that you don't have any other build errors. – mm8 Jan 11 '17 at 11:30
  • @mm8 - after some builds, re-builds and restarts, it finally worked. Thank you. – Jaroslaw Matlak Jan 11 '17 at 11:40

0 Answers0