Anuja
Anuja

Reputation: 11

Popup Move in WPF

I created a WPF Popup Control on my WPF Form.When I click,the Popup Opens and it also moves properly. but when I Clicked on textbox in popup window then popup moves slightly towards the edge of main window. I don't want to move popup when I clicked on textbox in Popup?

    private void MyAddJobPopup_MouseMove(object sender,System.Windows.Input.MouseEventArgs e) {
    if (!MyAddJobPopup.IsOpen)
       MyAddJobPopup.IsOpen = true;

    if (e.LeftButton == MouseButtonState.Pressed) {
            var mousePosition =   e.GetPosition(this.HardwareElectricalTbl);
         this.MyAddJobPopup.HorizontalOffset = mousePosition.X;
          this.MyAddJobPopup.VerticalOffset = mousePosition.Y;
        }
    }

Upvotes: 0

Views: 411

Answers (1)

Rafaeltab
Rafaeltab

Reputation: 171

When setting the HorizontalOffset and VerticalOffset they use the top left location of the window, which is not the location you want to set it to, you want to set it to the mouse position minus the mouses previous relative position to the window.

To do this save the following locations when you start clicking (for example inside of an onClick event):

//both should be negative so adding will actually remove from the location which is what you want
float offsetX = this.MyAddJobPopup.HorizontalOffset - mousePosition.X;
float offsetY = this.MyAddJobPopup.VerticalOffset- mousePosition.Y;

then when you set the location add them:

this.MyAddJobPopup.HorizontalOffset = mousePosition.X + offsetX;
this.MyAddJobPopup.VerticalOffset = mousePosition.Y + offsetY;

Upvotes: 1

Related Questions