Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-设置对话框窗口相对于用户控件的位置_C#_Wpf_User Controls_Location - Fatal编程技术网

C# WPF-设置对话框窗口相对于用户控件的位置

C# WPF-设置对话框窗口相对于用户控件的位置,c#,wpf,user-controls,location,C#,Wpf,User Controls,Location,我需要帮助设置对话框窗口相对于用户控件的位置 我想在窗口启动时在中间用户控件中显示我的窗口。< /P> 如何找到用户控件的左和右位置 我在我的应用程序中使用此代码,但在WPF中无法正常工作 谢谢你的帮助 private void PossitionWindow(object sender, RoutedEventArgs e) { Window wind = new Window(); var location = this.PointTo

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

<>我想在窗口启动时在中间用户控件中显示我的窗口。< /P> 如何找到用户控件的左和右位置

我在我的应用程序中使用此代码,但在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;
    }

这里有一个小例子

// 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();
预览

是否要在应用程序主窗口的中心显示对话框?否,我要在用户控件的中心显示对话框窗口。