如何在 WPF 中设置 child window 边界不超出 MainWindow
How to give a child window bounds to not go outside the MainWindow in WPF
我真的有一个简单的问题,但我似乎找不到它。我正在使用 WPF,我正在创建一个显示简单视图的 child window (Class Window)。现在我可以移动 window,但是我想要的是它不能移动到主 window 之外。 (如果我没记错的话,我认为 Popup 控件具有这种行为)。有什么简单的方法可以做到吗?
如果不清楚,请告诉我。
提前致谢。
亲切的问候,
Qwin
让你 parent window 成为 child window 的所有者并调整位置
public MainWindow()
{
InitializeComponent();
LocationChanged += new EventHandler(Window_LocationChanged);
}
ChildWindow win = new ChildWindow();
win.Owner = this;
win.Show();
private void Window_LocationChanged(object sender, EventArgs e)
{
Console.WriteLine("LocationChanged - ({0},{1})", this.Top, this.Left);
foreach (Window win in this.OwnedWindows)
{
win.Top = this.Top + 100;
win.Left = this.Left + 100;
}
}
我真的有一个简单的问题,但我似乎找不到它。我正在使用 WPF,我正在创建一个显示简单视图的 child window (Class Window)。现在我可以移动 window,但是我想要的是它不能移动到主 window 之外。 (如果我没记错的话,我认为 Popup 控件具有这种行为)。有什么简单的方法可以做到吗?
如果不清楚,请告诉我。
提前致谢。
亲切的问候, Qwin
让你 parent window 成为 child window 的所有者并调整位置
public MainWindow()
{
InitializeComponent();
LocationChanged += new EventHandler(Window_LocationChanged);
}
ChildWindow win = new ChildWindow();
win.Owner = this;
win.Show();
private void Window_LocationChanged(object sender, EventArgs e)
{
Console.WriteLine("LocationChanged - ({0},{1})", this.Top, this.Left);
foreach (Window win in this.OwnedWindows)
{
win.Top = this.Top + 100;
win.Left = this.Left + 100;
}
}