2011-04-07 57 views
2

我有以下問題:當畫布中的項目被選中(將被擦除)時,滾動查看器總是重置爲0:這是由於示例代碼中的焦點。如果焦點()被刪除,ScrollViewer中是好,但所選擇的項目現在不能被擦除>WPF:避免畫布滾動查看器重置

Mainwindow.Xaml代碼:

<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 

回答

2

你應該嘗試處理RequestBringIntoView引發的事件時該項目獲得焦點並阻止該事件冒泡到ScrollViewer。將這些事件標記爲處理的一個好地方是在Canvas級別。

+0

太棒了,這正是我需要的+1。按照建議在我的畫布'RequestBringIntoView'中設置'e.Handled = true',它可以很好地工作 – RobJohnson 2013-03-28 10:59:28