2011-05-17 10 views

回答

2

我真的很喜歡的效果也一樣,所以我重新它使用行爲:

public class PanBehavior : Behavior<ScrollViewer> 
{ 
    protected override void OnAttached() 
    { 
     this.AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove); 
     this.AssociatedObject.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden; 
     this.AssociatedObject.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; 
    } 

    void AssociatedObject_MouseMove(object sender, MouseEventArgs e) 
    { 
     var mousePos = e.GetPosition(this.AssociatedObject); 

     double panningY = this.AssociatedObject.ExtentHeight - this.AssociatedObject.ViewportHeight; 
     double panningX = this.AssociatedObject.ExtentWidth - this.AssociatedObject.ViewportWidth; 

     double relativeMouseY = mousePos.Y/this.AssociatedObject.ActualHeight; 
     double relativeMouseX = mousePos.X/this.AssociatedObject.ActualWidth; 

     panningY *= relativeMouseY; 
     panningX *= relativeMouseX; 

     this.AssociatedObject.ScrollToVerticalOffset(panningY); 
     this.AssociatedObject.ScrollToHorizontalOffset(panningX); 

    } 
} 

只需添加這種行爲任何的ScrollViewer,它只是工作。作爲一個例子,這個XAML:

<Grid> 
     <ScrollViewer> 
      <i:Interaction.Behaviors> 
       <local:PanBehavior /> 
      </i:Interaction.Behaviors> 
      <UniformGrid Width="1000" Height="1000" Rows="10" Columns="10"> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
       <Button Content="Hello" /> 
      </UniformGrid> 
     </ScrollViewer> 
    </Grid> 

請注意,我只在WPF測試這一點,但它應該工作幾乎在Silverlight相同。 - EDIT--用這兩種技術對它進行了測試。

+1對於很酷的問題! :)

+0

感謝您的答案。所以泛類會進入另一個文件,或者我可以把它放在mainPage文件中。@ Elad Katz – user758024 2011-05-19 00:26:12

+0

把它放在任何你想要的地方。 – 2011-05-19 00:27:51

+0

最後一個問題。我有前綴「本地」的問題。如何在xml代碼中聲明它?@Elad Katz – user758024 2011-05-19 10:45:22

相關問題