Invalid mouse events sent when using a ribbon popup gallery

Ribbon for WPF Forum

Posted 13 years ago by Keith I
Version: 11.1.0542
Platform: .NET 4.0
Environment: Windows 7 (64-bit)
Avatar
I have a gallery in a ribbon and I get unusual mouse events sent when I show the popup gallery and click on an item in it. This affects drag and drop from the gallery.

I have modified your GalleryInRibbon sample to show the problem by making the changes detailed below:

In MainWindow.xaml, change the DataTemplate used as the ribbon:RibbonGallery.ItemTemplate to be:

<DataTemplate>
   <Image Margin="2" Source="{Binding BindsDirectlyToSource=True}" Stretch="None" MouseDown="GalleryImage_PreviewMouseLeftButtonDown" MouseMove="GalleryImage_PreviewMouseMove"/>
</DataTemplate>
Add the file MainWindow.xaml.cs with the following code:

using System;
using System.Windows;

namespace ActiproSoftware.Windows.ProductSamples.RibbonSamples.QuickStart.GalleryInRibbon
{
    /// <summary>
    /// Provides the main window for this sample.
    /// </summary>
    public partial class MainWindow : ActiproSoftware.Windows.Controls.Ribbon.RibbonWindow
    {
        /// <summary>
        /// Initializes an instance of the <c>MainWindow</c> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        private void GalleryImage_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Point position = e.GetPosition(null);
            Console.WriteLine("GalleryImage_PreviewMouseDown X=" + position.X.ToString() + " Y=" + position.Y.ToString() + " sender=" + sender.GetHashCode().ToString());
        }

        private void GalleryImage_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Point position = e.GetPosition(null);
            Console.WriteLine("GalleryImage_PreviewMouseMove X=" + position.X.ToString() + " Y=" + position.Y.ToString() + " sender=" + sender.GetHashCode().ToString());
        }
    }
}
When you then run the application, click to show the popup window of the gallery and then press-and-hold-down the left mouse button on an item in the popup. In the output window you will see the console logging. This will show something like the following:
GalleryImage_PreviewMouseDown X=466 Y=138 sender=1556506
GalleryImage_PreviewMouseMove X=462 Y=78 sender=1556506
You will see that a mouse-move event is sent immediately (even when the mouse doesn't move) and one or both of the X and Y values will be considerably different in the mouse-move event to that in the mouse-down event.

If you press-and-hold-down the left mouse button on the gallery when it is not showing the popup gallery, then you only get the mouse-down event.

The reason that this causes a problem is that it breaks drag-and-drop. Most drag-and-drop operations are started when X changes by SystemParameters.MinimumHorizontalDragDistance or Y changes by SystemParameters.MinimumVerticalDragDistance. I get a very weird effect relating to focus because drag-drop is started from the popup gallery as soon as you hold the left mouse button down.

Thanks in advance.

Comments (2)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Keith,

This isn't a bug or anything. It's due to how WPF works and this line of code you had:
Point position = e.GetPosition(null);
That will get the position relative to the current element that has mouse capture. As you press the mouse button down, capture is in the midst of changing to the Button in the GalleryItem and it's triggering a MouseMove event. That all is happening without any of our code in the stack. Since the mouse capture element is changing right at that spot, the GetPosition call returns a different origin, but oddly enough the Mouse.Captured property hasn't yet been updated at that point.

To work around the issue you should do this in your handlers instead. This ensures a consistent result:
Point position = e.GetPosition((IInputElement)sender);


Actipro Software Support

Posted 13 years ago by Keith I
Avatar
Thank you very much for your reply. Your fix works perfectly.

Once again, excellent support from Actipro.
The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.