Handling alternative input methods

Apart from support for the good old left mouse button, Silverlight 4 has added support for the right mouse click and mouse wheel as well. Up until Silverlight 4, whenever you tried to right-click on an application, you were getting the Silverlight options menu. With the introduction of Silverlight 4, however, we can now override this default behavior and implement our own logic. In RIA applications, you would usually want to display a contextual menu.

Just like its left button sibling, handling the right mouse button can be done by handling the events of MouseRightButtonDown and MouseRightButtonUp. In order to prevent the default Silverlight options menu from showing up, you have to handle the MouseRightButtonDown event, and set the Handled property of the MouseButtonEventArgs object to true.

Open the Chapter6-RightClick project from the downloadable content. The project contains a simple Border control. Handlers were added on the Border control for both the MouseRightButtonDown and MouseRightButtonUp controls. Switch over to the MainPage.xaml.cs file. As stated in the preceding paragraph, we first have to prevent the default Silverlight menu to show up. Add the following line of code to the rightBrd_MouseRightButtonDown method:

e.Handled = true;

The ContextMenu class represents a context menu, just like Silverlight's default. Menu items are represented by the MenuItem class. In order to create such a contextual menu, we have to first create an object from the ContextMenu class, then create a few MenuItem instances, and finally add them to the ContextMenu object we have created previously. Add the following code snippet to the rightBrd_MouseRightButtonUp method:

_menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Header = "Test item 1";
_menu.Items.Add(item);
MenuItem item2 = new MenuItem();
item2.Header = "Test item 2";
_menu.Items.Add(item2);
MenuItem item3 = new MenuItem();
item3.Header = "Test item 3";
_menu.Items.Add(item3);
_menu.HorizontalOffset = e.GetPosition(this).X;
_menu.VerticalOffset = e.GetPosition(this).Y;
_menu.IsOpen = true;

Notice how both the HorizontalOffset and VerticalOffset properties of the ContextMenu object have to be defined, otherwise the Silverlight rendering engine won't know where to position the contextual menu. Using the GetPosition method of the MouseButtonEventArgs object enables us to get the current position of the mouse on both the x and y coordinates.

That's it! Build and run the application, and you should get the following result after right-clicking on the Border control:

Handling alternative input methods

Handling the mouse wheel

Just like the mouse buttons, Silverlight allows us to react to the mouse wheel as well. In order to handle the mouse wheel, you have to handle the MouseWheel event of a control. For example, to handle the MouseWheel event of the rightBrd control in the previous example, you can add the following line of code in the constructor method of MainPage:

rightBrd.MouseWheel+=new MouseWheelEventHandler(rightBrd_MouseWheel);

Inside the rightBrd_MouseWheel method, you can access the Delta property of the MouseWheelEventArgs object, which is a double type of object representing the "amount" of scroll by which the user has scrolled.

I encourage you to check Laurent Duveau's great blog post on handling the mouse wheel event located at http://weblogs.asp.net/lduveau/archive/2009/11/20/silverlight-4-mouse-wheel-support.aspx.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.12.34