【问题标题】:WPF - Set dialog window position relative to user controlWPF - 设置相对于用户控件的对话框窗口位置
【发布时间】:2017-10-22 11:41:00
【问题描述】:

我需要有关设置对话框窗口相对于用户控件的位置的帮助。

我想在窗口启动时在中间用户控件中显示我的窗口。

如何找到我的用户控件的 left 和 tom 位置?

我在我的应用程序中使用此代码,但在 WPF 中无法正常工作。

感谢您的帮助。

     private void PossitionWindow(object sender, RoutedEventArgs e)
      {
        Window wind = new Window();

        var location = this.PointToScreen(new Point(0, 0));
        wind.Left = location.X;
        wind.Top = location.Y - wind.Height;
        location.X = wind.Top + (wind.Height - this.ActualHeight) / 2;
        location.Y = wind.Left + (wind.Width - this.ActualWidth) / 2;
    }

【问题讨论】:

  • 您想在应用程序主窗口的中心显示对话框吗?
  • 不,我想在我的用户控件中心显示对话框窗口。

标签: c# wpf user-controls location


【解决方案1】:

这是一个小例子。

// Get absolute location on screen of upper left corner of the UserControl
Point locationFromScreen =  userControl1.PointToScreen(new Point(0, 0));

// Transform screen point to WPF device independent point
PresentationSource source = PresentationSource.FromVisual(this);
Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);

// Get Focus
Point focus = new Point();
focus.X = targetPoints.X + (userControl1.Width / 2.0);
focus.Y = targetPoints.Y + (userControl1.Height / 2.0);

// Set coordinates

Window window = new Window();
window.Width = 300;
window.Height = 300;
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Top = focus.Y - (window.Width / 2.0);
window.Left = focus.X - (window.Height / 2.0);

window.ShowDialog();

预览

【讨论】:

    猜你喜欢
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2010-10-11
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多