parfum
parfum

Reputation: 347

I want to place a child user control on the left side of a parent user control in WPF

enter image description here

There are two user controls.

For convenience, black is a parent and red is a child.

As shown in the following figure, I want to get the parent's window position and place the child window.

I've done a lot of searching and tested the location, but the parent's location was null.

I want to place a child window like red.

Here is a part of my code.

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Copsys.Comm
{        
    public partial class dlgNoticeInputControl : Window
    {
        public dlgNoticeInputControl()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CommControl com = new CommControl();

            //not parent location data...
            double parentWidth = com.Width; //parent(MainForm) Width
            double parentHeight = com.Height; //parent(MainForm) Height


            double childWidth = this.Width;  //child(Form1) Width
            double childHeight = this.Height; //child(Form1) Height


            Point positions = this.PointToScreen(new Point(parentWidth - childWidth - 10, parentHeight - childHeight - 10));

        }
    }
}

If there is a better way than the code I wrote, please teach me.

Upvotes: 2

Views: 179

Answers (2)

Kang Dong Gyun
Kang Dong Gyun

Reputation: 65

element.PointToScreen(new Point(0, 0));

Let’s create a simple window with a few buttons. Each button will show it’s screens location when use clicks it. This is our Window.

<WrapPanel>
    <Button>Button 1</Button>
    <Button>Button 2</Button>
    <Button>Button 3</Button>
    <Button>Button 4</Button>
    <Button>Button 5</Button>
    <Button>Button 6</Button>
    <Button>Button 7</Button>
    <Button>Button 8</Button>
    <Button>Button 9</Button>
    <Button>Button 10</Button>
</WrapPanel>

I am using the same UIElement.MouseDown event’s handler for all these buttons. The easiest way to do this is using EventManager.RegisterClassHandler method. It allows you to register a class handler for a particular routed event. This is the code I have added to the Window’s class constructor.

EventManager.RegisterClassHandler(typeof(Button), MouseDownEvent, new RoutedEventHandler(OnMouseDown));

The rest of the application is easy: get a button and show its location.

 private void OnMouseDown(object sender, RoutedEventArgs e)
 {
     var element = sender as ContentControl;
     if (element != null)
     {
         ShowLocation(element);
     }
 }

private void ShowLocation(ContentControl element)
{
    var location = element.PointToScreen(new Point(0, 0));
    MessageBox.Show(string.Format(
                                    "{2}'s location is ({0}, {1})", 
                                    location.X, 
                                    location.Y, 
                                    element.Content));
}

Upvotes: 2

강동균
강동균

Reputation: 64

I've been considering how to manually find it for you.

I manually look up the position value manually.

Try moving the window by giving the position value directly.

The current code calculates the x and y axis of the button and fixes the position value by giving + -.

private void button1_Click(object sender, RoutedEventArgs e)
{
    var dlg = new dlgNoticeInputControl();
    Console.WriteLine(((Button)sender).Margin);
    //MessageBox.Show(""+((Button)sender).PointToScreen(new Point(0, 0)));
    dlg.Left = ((Button)sender).PointToScreen(new Point(0, 0)).X - 580;
    dlg.Top = ((Button)sender).PointToScreen(new Point(0, 0)).Y - 500;
    dlg.Show();

}

Upvotes: 2

Related Questions