public static class SetShowCenterWindowHelper
{public static void SetShowCenterWindow(Window targetWindow, Window owner){targetWindow.WindowStartupLocation = WindowStartupLocation.Manual;// 获取主窗口的位置和大小var ownerPosition = owner.Position;var ownerWidth = owner.Width;var ownerHeight = owner.Height;// 获取对话框的尺寸(确保在调用前已设置Width和Height,或使用ActualWidth/ActualHeight)var dialogWidth = targetWindow.Width;var dialogHeight = targetWindow.Height;// 计算居中坐标var centerX = ownerPosition.X + (ownerWidth - dialogWidth) / 2;var centerY = ownerPosition.Y + (ownerHeight - dialogHeight) / 2;// 应用计算出的位置targetWindow.Position = new PixelPoint((int)centerX, (int)centerY);}
}