Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:05:14+00:00 2026-05-26T10:05:14+00:00

The following code is supposed to move UIElements in a canvas, when the mouse

  • 0

The following code is supposed to move UIElements in a canvas, when the mouse hovers over them, and the user hits Ctrl.

void keydown(Object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
            {
                control++;
                if (control == 1)
                {
                    drag = true;
                    elem = (UIElement)Mouse.DirectlyOver;
                }
                else
                    drag = false;
                control %= 2;
            }
        }

        void mousemove(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(canvas);
            if (drag)
            {
                if (elem == null) return;
                //Canvas.SetLeft(myButton, p.X);  <-- this works, but then why doesn't it work when I generalize it?
                Canvas.SetLeft(elem, p.X);
                Canvas.SetTop(elem, p.Y);
            }
        }

Any Shapes component, e.g. Rectangles will move when I hover the mouse over them, and hit control..But it doesn’t work with Buttons, TextBoxes, TextViews, etc. Can anyone explain?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:05:15+00:00Added an answer on May 26, 2026 at 10:05 am

    The documentation for Mouse.DirectlyOver says:

    Controls can be composed of multiple elements. DirectlyOver reports the specific element in the composite control the mouse pointer is over and not the control itself. For example, depending on which part of a Button the pointer is over, the DirectlyOver property could report the TextBox of the Content property or the ButtonChrome.

    In other words: A Button is made up of several sub-elements, like a ButtonChrome and a TextBlock (usually not a TextBox — I think that’s a typo on the MSDN page). When you call Mouse.DirectlyOver, you’re probably getting one of those elements, rather than the Button.

    Since those elements are not parented to a Canvas (they’re parented to something in the Button’s control template, most likely a Grid), setting the Canvas.Left and Canvas.Top attached properties will have no effect.

    You probably want to walk up the visual tree (using VisualTreeHelper.GetParent) until you find something you’re interested in dragging. How you determine whether you’re interested in any given element is up to you. You could go until you find something that’s parented to your Canvas, or you could just go until you find something that’s one of a given set of types (stopping when you find something that descends from Control might be a decent place to start).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a SMS app the following code is supposed to: check if
Given the following code (it's supposed to write helloworld in a helloworld file, and
I expected the following code to print 8, 111 and 999. I supposed that
I have the following pages: page1.php, page2.php and page3.php. Code in each of them
The following code is supposed to show in a message box the index of
The following code gives an error when it's supposed to output just std::endl :
I have the following code, which is supposed to try to parse a given
I have the following code. However I get a error. How is this supposed
The following code supposed to read a file containing a set of molecular structures,
The following code is supposed to perform an AJAX request after the page loads,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.