0

I have a problem with my TreeView and DataGrid. The issue is that whenever I click an item in my TreeView (or DataGrid), the view horizontally scrolls to the right, I'm guessing, in a bid to make it more visible. I've got some images of my TreeView below:

Before Item Click:

Before Item Click

After Item Click

After Item Click

I'm sure I could disable the Horizontal scrollbar, but I would still like the user to have the option to scroll, just not auto-scroll when an item is clicked.

Any help on this is greatly appreciated and just for the sake of completeness, I've attached my TreeView code below:

<TreeView x:Name="TreeView" 
          ItemsSource="{Binding Path=FileStubsView}"
          SelectedItemChanged="TreeView_OnSelectedItemChanged"
          common:MultipleSelectTreeView.PreviewMouseDoubleClick="HandleTreeItemDoubleClick"
          Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}"
          Grid.Row="1" 
          Grid.Column="0" 
          Margin="0,2,0,0" 
          TreeViewItem.Expanded="TreeViewItem_Expanded" 
          TreeViewItem.Collapsed="TreeViewItem_Collapsed" >
Kcvin
  • 5,073
  • 2
  • 32
  • 54
Black Dynamite
  • 4,067
  • 5
  • 40
  • 75
  • This is a similar question with accepted answer. http://stackoverflow.com/questions/2064848/wpf-datagrid-how-do-i-stop-auto-scrolling-when-a-cell-is-clicked – Nikhil Vartak Nov 25 '15 at 18:40

1 Answers1

1

Here is a working solution which derives from the answer in this question:

Code-behind

public partial class MainWindow : Window
{
    public List<Item> Items
    {
        get
        {
            return new List<Item>()
            {
                new Item(),
                new Item(),
                new Item(),
                new Item(),
            };
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    private void TreeViewItem_CancelRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
    {
        e.Handled = true;
    }
}

Model

public class Item
{
    public string Text
    {
        get
        {
            return "Itemaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        }
    }

    public List<Item> Items
    {
        get
        {
            return new List<Item>()
            {
                new Item(),
                new Item(),
                new Item(),
            };
        }
    }
}

View

<Grid Width="200">
    <TreeView ItemsSource="{Binding Items}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Items}">
                <TextBlock Text="{Binding Text}"/>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <EventSetter Event="Control.RequestBringIntoView"
                             Handler="TreeViewItem_CancelRequestBringIntoView" />
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>
</Grid>

Output

Proof

Community
  • 1
  • 1
Kcvin
  • 5,073
  • 2
  • 32
  • 54