Zooming with the mouse wheel in Silverlight

Declare grid within viewbox:

<UserControl x: Class="Examples.MouseWheelZoom" 
 xmlns="http: //schemas. microsoft. com/winfx/2006/xaml/presentation"  
 xmlns: x="http: //schemas. microsoft. com/winfx/2006/xaml"  
MouseWheel="Page_MouseWheel"> 
 
  <ScrollViewer VerticalScrollBarVisibility="Auto" 
   HorizontalScrollBarVisibility="Auto"> 
 
    <Viewbox x: Name="viewbox" Height="250" Width="350"> 
      <Grid Background="White" Height="250" Width=" 350"> 
        . .. 
      </Grid> 
    </Viewbox> 
 
  </ScrollViewer> 
</UserControl>

Code behind:

private void Page_MouseWheel(object sender, MouseWheelEventArgs e) 
{ 
    // The Delta is in units of 120, so dividing by 120 gives 
    // a scale factor of 1. 09 (120/110) . In other words, one 
    // mouse wheel notch expands or shrinks the Viewbox by about 9%. 
    double scalingFactor = (double) e. Delta / 110; 
 
    // Check which way the wheel was turned. 
    if (scalingFactor > 0) 
    { 
        // Expand the viewbox. 
        viewbox.Width *= scalingFactor; 
        viewbox.Height *= scalingFactor; 
    } 
    else 
    { 
        // Shrink the viewbox. 
        viewbox.Width /= -scalingFactor; 
        viewbox.Height /= - scalingFactor; 
    } 
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.