Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
The CaptureMouse() call doesn't appear to be working for the MouseRightButtonDown event in my Silverlight 4 application. The code below shows the following:
(1) When holding down the left mouse button inside the IE web browser window, and keeping the button down until the mouse is moved outside the web browser window, if you release the left mouse button at this point, the function UserControl_MouseLeftButtonUp will be reached.
(2) When holding down the right mouse button inside the IE web browser window, and keeping the button down until the mouse is moved outside the web browser window, if you release the right mouse button at this point, the function UserControl_MouseRightButtonUp will not be reached.

Why won't UserControl_MouseRightButtonUp be reached?

HTML
<UserControl x:Class="sidebar.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" 
             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:my="clr-namespace:sidebar"
             MouseLeftButtonDown="UserControl_MouseLeftButtonDown"
             MouseLeftButtonUp="UserControl_MouseLeftButtonUp"
             MouseRightButtonDown="UserControl_MouseRightButtonDown"
             MouseRightButtonUp="UserControl_MouseRightButtonUp"
             >

    <Grid x:Name="LayoutRoot" removed="White">
    </Grid>
</UserControl>

--------------------------------------------------------------------------------------

C#
using System.Windows.Controls;
using System.Windows.Input;

namespace sidebar
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            CaptureMouse();
        }

        private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("LeftMouseUp");
        }

        private void UserControl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            CaptureMouse();
            e.Handled = true;
        }

        private void UserControl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("RightMouseUp");
        }
    }
}
Posted
Updated 14-Mar-12 9:20am
v5
Comments
Member 8169986 4-Apr-12 10:53am    
I thought I've lost my mind until seeing your question. Handle the LostMouseCapture event and you'll see that as soon as you move mouse while holding down the right mouse button, the event fires.

I wouldn't consider the issue solved because there's no reason leftbutton behavior should differ from the right.
mla154 4-Apr-12 11:00am    
As far as I'm concerned, it's solved. Since you believe left and mouse button behavior shouldn't differ, I would suggest that you contact Microsoft about it.

1 solution

This book from Google books (http://books.google.com/books?id=PJmgeIdZT3YC&pg=PA117&lpg=PA117&dq=silverlight+mouse+%22capture%22+right+button&source=bl&ots=VtN1mnkpsa&sig=yHvxHw03Wbg7XDpjusGclYDKgJw&hl=en&sa=X&ei=z_FgT7rPKcO2gwf5guiWCA&ved=0CFsQ6AEwCQ#v=onepage&q=capture&f=false[^]), page 127, seems to suggest that mouse capture can be done by the left mouse button and not the right mouse button. If anyone can prove me wrong, please do!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900