How to Fix Mah.Apps Metro Style Paronama problem On Mouse click Event

Previously in part 1, show how to use panorama effect via MahApps Window 8 Metro Style to view it please click here

Mah.Apps.Metro paronama effect have a small bugs where the mouse click event on other object such as button have no fired. This post will how to fix this problem by editing the Mah.Apps.Metro paronama.cs code.

Here is the code edited

     protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            if (sv.IsMouseOver)
            {
                tile = null;

                // Save starting point, used later when determining how much to scroll.
                scrollStartPoint = e.GetPosition(this);
                scrollStartOffset.X = sv.HorizontalOffset;
                scrollStartOffset.Y = sv.VerticalOffset;

                // Update the cursor if can scroll or not.
                Cursor = (sv.ExtentWidth > sv.ViewportWidth) || (sv.ExtentHeight > sv.ViewportHeight) ?Cursors.ScrollAll : Cursors.Arrow;

                //store Control if one was found, so we can call its command later
                var x = TreeHelper.TryFindFromPoint<ListBoxItem>(this, scrollStartPoint);
                if (x != null)
                {
                    x.IsSelected = true;
                    ItemsControl tiles = ItemsControlFromItemContainer(x);
                    var data = tiles.ItemContainerGenerator.ItemFromContainer(x);
                    if (data != null && data is IPanoramaTile)
                    {
                        tile = (IPanoramaTile)data;
                    }
                }

              //CaptureMouse();
            }

            base.OnPreviewMouseDown(e);
        }

        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            //if (IsMouseCaptured)
            //{
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point currentPoint = e.GetPosition(this);

                // Determine the new amount to scroll.
                var delta = new Point(scrollStartPoint.X - currentPoint.X, scrollStartPoint.Y - currentPoint.Y);

                if (Math.Abs(delta.X) < PixelsToMoveToBeConsideredScroll &&
                    Math.Abs(delta.Y) < PixelsToMoveToBeConsideredScroll)
                    return;

                scrollTarget.X = scrollStartOffset.X + delta.X;
                scrollTarget.Y = scrollStartOffset.Y + delta.Y;

                // Scroll to the new position.
                sv.ScrollToHorizontalOffset(scrollTarget.X);
                sv.ScrollToVerticalOffset(scrollTarget.Y);
                CaptureMouse();
            }

            base.OnPreviewMouseMove(e);
        }

        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            if (IsMouseCaptured)
            {
               // Cursor = Cursors.Arrow;
                ReleaseMouseCapture();
            }
            Cursor = Cursors.Arrow;
            Point currentPoint = e.GetPosition(this);

            // Determine the new amount to scroll.
            var delta = new Point(scrollStartPoint.X - currentPoint.X, scrollStartPoint.Y - currentPoint.Y);

            if (Math.Abs(delta.X) < PixelsToMoveToBeConsideredClick &&
                Math.Abs(delta.Y) < PixelsToMoveToBeConsideredClick && tile != null)
            {
                if (tile.TileClickedCommand != null)
                    //Ok, its a click ask the tile to do its job
                    if (tile.TileClickedCommand.CanExecute(null))
                    {
                        tile.TileClickedCommand.Execute(null);
                    }
            }

            base.OnPreviewMouseUp(e);
        }
Advertisements

One comment


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s