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

时间:2017-04-10 02:47:48

标签: 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.

2 个答案:

答案 0 :(得分:2)

我一直在考虑如何为您手动找到它。

我手动手动查找位置值。

尝试直接给出位置值来移动窗口。

当前代码计算按钮的x轴和y轴,并通过给出+ - 来修正位置值。

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();

}

答案 1 :(得分:2)

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

让我们创建一个带有几个按钮的简单窗口。使用时,每个按钮都会显示它的屏幕位置。这是我们的窗口。

<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>

我正在为所有这些按钮使用相同的UIElement.MouseDown事件处理程序。最简单的方法是使用EventManager.RegisterClassHandler方法。它允许您为特定的路由事件注册类处理程序。这是我添加到Window的类构造函数中的代码。

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

应用程序的其余部分很简单:获取一个按钮并显示其位置。

 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));
}