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 993645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:27:21+00:00 2026-05-16T06:27:21+00:00

I have a custom WPF control which consist of single TextBox <UserControl HorizontalAlignment=Left x:Class=WPFDiagramDesignerControl.Components.UcWBSBlock

  • 0

I have a custom WPF control which consist of single TextBox

<UserControl HorizontalAlignment="Left" x:Class="WPFDiagramDesignerControl.Components.UcWBSBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="100" Width="100" IsEnabled="True">

    <Grid >
        <TextBox x:Name="txtBox" IsEnabled="True"   Background="AntiqueWhite"  Margin="10,10,10,10" TextWrapping="Wrap"> </TextBox>
    </Grid>

</UserControl>

The control is placed on Canvas called MyDesigner.

I want to move my control on canvas only if I click on textbox and move mouse. I don’t want to move control when I click on margin between borders of control and textbox.
I started writting a code and it looks like that

public partial class UcWBSBlock : UserControl
{

    bool textChanged = false;
    bool isClicked = false;
    Point startPoint;
    DesignerItem parentItem;
    DesignerCanvas parentCanvas;

    public UcWBSBlock()
    {

        InitializeComponent();

        txtBox.MouseDoubleClick+=new MouseButtonEventHandler(txtBox_MouseDoubleClick);
        txtBox.MouseMove+=new MouseEventHandler(txtBox_MouseMove);
        txtBox.PreviewMouseDown+=new MouseButtonEventHandler(txtBox_PreviewMouseDown);
        txtBox.PreviewMouseUp+=new MouseButtonEventHandler(txtBox_PreviewMouseUp);
        txtBox.Cursor = Cursors.SizeAll;

    }

    private void txtBox_MouseMove(object sender, RoutedEventArgs e)
    {
        if (isClicked)
        {
            Point mousePos = Mouse.GetPosition(parentCanvas);

            parentItem = this.Parent as DesignerItem;
            parentCanvas = parentItem.Parent as DesignerCanvas;
            Point relativePosition = Mouse.GetPosition(parentCanvas);
            DesignerCanvas.SetLeft(parentItem,DesignerCanvas.GetLeft(parentItem) -  (startPoint.X - mousePos.X));
            DesignerCanvas.SetTop(parentItem, DesignerCanvas.GetTop(parentItem) - (startPoint.Y - mousePos.Y));
        }  
    }

    private void txtBox_PreviewMouseDown(object sender, RoutedEventArgs e)
    {
        if (!isClicked)
        {
            isClicked = true;
            parentItem = this.Parent as DesignerItem;
            parentCanvas = parentItem.Parent as DesignerCanvas;
            startPoint = Mouse.GetPosition(parentCanvas);
        }
    }

    private void txtBox_PreviewMouseUp(object sender, RoutedEventArgs e)
    {
        isClicked = false;
    }
}

}

However my control doesn’t move 🙁 What did I do wrong ?? It’s hard to debug this 🙂

  • 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-16T06:27:23+00:00Added an answer on May 16, 2026 at 6:27 am

    you are setting the left/top of the parent item, not your control:

    DesignerCanvas.SetLeft(parentItem,DesignerCanvas.GetLeft(parentItem) -  (startPoint.X - mousePos.X));
    DesignerCanvas.SetTop(parentItem, DesignerCanvas.GetTop(parentItem) - (startPoint.Y - mousePos.Y));
    

    should (probably) be like this:

    DesignerCanvas.SetLeft(this,DesignerCanvas.GetLeft(this) -  (startPoint.X - mousePos.X));
    DesignerCanvas.SetTop(this, DesignerCanvas.GetTop(this) - (startPoint.Y - mousePos.Y));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my WPF project I have a custom control which is visually represented by
I have a Custom control in WPF in which I define a large ItemsControl
I have created a custom WPF user control which is intended to be used
In WPF, I have a custom control that inherits from TreeView. The code is
I have a WPF application which has a main window composed from several custom
I'm putting a WPF application together in which I have an image control which
I have a custom control in WPF that simply holds a combo box (however
I have a WinForms usercontrol hosting a WPF custom Listbox in it. After the
I have a custom WPF user control called a TimeoutPanel that I am trying
I have a WPF Custom Control I defined in a WPF Control Library project

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.