Silverlight 2 Drag and Scroll Image

This is somewhat based on Shawn Wildermuth’s blog post for drag and drop. I had two problems with his post.

  1. I’m not scrolling a canvas.
  2. If your mouse leaves the control with the button down you are still dragging when your mouse reenters the control.

So here’s my tweaked version:

The control has a scroll viewer as the root element and a simple grid with one column and one row inside that. There is a large image inside the grid cell. There are events firing from the grid’s mouse down, up, move and grid leave events.

<UserControl x:Class="TestMouseScroll.Page"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="300" Height="300" Background
="Transparent" >
  <ScrollViewer
    x:Name="scrollViewImage"
    HorizontalScrollBarVisibility="Visible"
    VerticalScrollBarVisibility
="Visible" >
    <Grid
      x:Name="gridImageContainer"
      Background="Gray"
      ShowGridLines
="True"
      MouseLeftButtonDown
="grid_MouseLeftButtonDown"
      MouseLeftButtonUp="grid_MouseLeftButtonUp"
      MouseMove
="grid_MouseMove"
      MouseLeave
="grid_MouseLeave"
   
  >
      <Grid.ColumnDefinitions
>
        <ColumnDefinition></ColumnDefinition
>
      </Grid.ColumnDefinitions
>
      <Grid.RowDefinitions
>
        <RowDefinition></RowDefinition
>
      </Grid.RowDefinitions
>
      <Image Source
=http://www.inetres.com/gp/military/infantry/rifle/M107/M107_1.jpg
         Grid.Row="0" Grid.Column="0" >
      </
Image
>
    </Grid
>
  </ScrollViewer
>
</
UserControl
>

The code looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TestMouseScroll
{
  public partial class Page : UserControl
  {

    bool isTracked = false;
    Point startDrag;

    public Page()
    {
      InitializeComponent();
    }

    private void grid_MouseLeftButtonDown(object sender,
      MouseButtonEventArgs e)
    {
      isTracked = true;
      // Get the starting mouseposition based on
      // where the mouse is positioned on the image,
      // not the relative postion inside the scroller
      startDrag = e.GetPosition(gridImageContainer);
    }

    private void grid_MouseLeftButtonUp(object sender, 
      MouseButtonEventArgs e)
    {
      isTracked = false;
    }

    private void grid_MouseLeave(object sender, MouseEventArgs e)
    {
      isTracked = false;
    }

    private void grid_MouseMove(object sender, MouseEventArgs e)
    {
      if (isTracked)
      {
        Point newPos = e.GetPosition(scrollViewImage);
        try
        {
          scrollViewImage.ScrollToHorizontalOffset(startDrag.X - newPos.X);
          scrollViewImage.ScrollToVerticalOffset(startDrag.Y - newPos.Y);
        }
        catch { }
      }
    }
  }
}

I think this is pretty straight forward and works nicely for me.

Incidentally, the Barrett was a sniper rifle introduced into the T/E of my unit some time between ‘91 and ‘93. I don’t know exactly when but it wasn’t there the first time I deployed and it was the second time. Used primarily as a vehicular countermeasure, it is still in use today. It’s an awesome addition to any infantry or STA unit firing accurately in the multi-kilometer range (v/s ~1,000 meters for standard 7.62). Note that the kevlar helmet has a USMC Corporal’s rank insignia on it. That’s E4. So a 21 year old professional warrior is trained and deployed with this little monster. Happens every day. There are people who have served in Congress and the House for many decades who cannot be trusted with a spitwad in a McDonalds straw but they have the power to send these fine gentlemen into harms way to voluntarily perform honorably and under stresses and conditions that would make normal men soil themselves. My point is that there ought to be a qualification process to be allowed to make our laws similar to the qualification it takes to become a Marine Sniper. An old Marine can wish…

2 Comments

  • Indeed.

    The problem is, the qualifications you see as important are not the quals I think are important. The framers of my constitution felt it was important that regular guys (including plumbers) were in elected into place to do the public works of the nation. Part of the problem has become that it's so expensive to get into office that only the rich and elite can get there and then they assign to subordinate positions of power only those who give them the 'perks' they have come to believe they deserve.

    As always happens throughout history, human nature does not change. Absolute power corrupts absolutely. Governments only get bigger.

    The 'smartest people in the room' often turn out to be self-appointed. These 'wizards of smart' begin to think that because they thought it up in the professors lounge it must be correct. Which examples do you want to prove that they are usually wrong and sometimes insane?

    Another problem is entitlements and their punitive nature on the productive. My President said on the campaign trail that health care is a right. I carry a copy of the Constitution of the United States of America with me wherever I go and I've read it many times. It doesn't say anything about health care. It may be correct and moral to be sure that it is provided to all CITIZENS but it is certainly not a right.

    Same story for people who can't afford to buy a house. The government steps in to make sure they can get it anyway and when the occupant realizes they can't afford it, they get out, declare bankruptcy and get an apartment. Oh wait, no they don't. They stay in the house and I pay for it with a confiscatory progressive tax code. This is 'fair' since I've got a better education, more money and am wildly rich and can afford it. No wait, that part isn't true either... Hmmmmm

  • Lots of well thought out things here (even the Silverlight stuff too). Jim, I think you'd really like reading the original "Starship Trooper" by Heinlein if you have not already.

    It would be easy to implement - and most countries do require this of all their male citizens already (just make it apply to females too due to the 19th Amendment) - all US citizens would have to serve 1 or 2 years of miltary or civil service to be eligible to vote (and hold office) with no option to buy your way out of it. After 4 or 5 decades everyone who is in office will have served in one way or another.

    Sounds very American to me.

    What I love about this country is people are allowed to disagree and peacefully discuss all the points of view.

Comments have been disabled for this content.