Search code examples
wpfmvvmscrollitemscontrol

Avoid scrolling following the focus on MVVM


I have the following ItemsControl:

<ItemsControl ItemsSource="{Binding Model.XList}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <StackPanel Orientation="Horizontal"></StackPanel>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

On each of them applies the following template:

<DataTemplate DataType="{x:Type model:MyModelType}">
    <Border MaxWidth="140">
        <local:SingleView DataContext="{Binding}"/>
    </Border>
</DataTemplate>

Actually makes each of them to "SingleView" control.

The ItemsControl wrapped in ScrollViewer.

When I have a large number of items (the scroll seems), and I press any button of the controls, the screen jumps (probably because the scroll getting the focus has changed).

I want to avoid scrolling following the focus.

I've seen solutions that make it through the Code-Behind, by RequestBringIntoView event.

But I'm working with MVVM, does anyone have a solution for this?


Solution

  • What helped me is to avoid receiving focus on ItemsControl

    (if there's a wrapper to ItemsControl so you can avoid getting focus also there)

    <ItemsControl ItemsSource="{Binding Model.XList}" Focusable="False">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal"></StackPanel>
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
    </ItemsControl>