Karl Arris
Karl Arris

Reputation: 21

WPF: Avoid Canvas scrollviewer reset

I've the following problem: when an item in the canvas is selected (to be erased), the scrollviewer is always reset to 0: this is due to the focus in the example code. If the focus() is removed, the scrollviewer is Ok, but the selected item now can't be erased!>

Mainwindow.Xaml code:

<Border Grid.Row="1" BorderThickness="1" BorderBrush="Navy" Margin="2" Padding="2" >
                <ScrollViewer Name="Posizione_scrollbar"  HorizontalScrollBarVisibility="Auto" 
                          VerticalScrollBarVisibility="Auto">


Protected Overrides Sub OnPreviewMouseDown(ByVal e As System.Windows.Input.MouseButtonEventArgs)
        MyBase.OnPreviewMouseDown(e)

        ' usual selection business
        Dim designer As DesignerCanvas = TryCast(VisualTreeHelper.GetParent(Me), DesignerCanvas)
        If designer IsNot Nothing Then
            If (Keyboard.Modifiers And (ModifierKeys.Shift Or ModifierKeys.Control)) <> ModifierKeys.None Then
                If Me.IsSelected Then
                    designer.SelectionService.RemoveFromSelection(Me)
                Else
                    designer.SelectionService.AddToSelection(Me)
                End If
            ElseIf Not Me.IsSelected Then
                If MainViewModel.Instance.ActiveDiagram.STMonitor = False Then
                    designer.SelectionService.SelectItem(Me)
                End If
            End If

            'Here is the problem: the canvas scrollbar is resetted to 0!
            Me.Focus()

        End If

        'True per avere la gestione col tasto sinistro del mouse
        e.Handled = True

    End Sub

Upvotes: 2

Views: 604

Answers (1)

mircea11
mircea11

Reputation: 21

You should try to handle RequestBringIntoView event raised when the item receives focus and prevent this event to be bubbled to ScrollViewer. One good place to mark these events as handled is at Canvas level.

Upvotes: 2

Related Questions