WPF:以编程方式设置 child window 大小
WPF : Set child window size programmatically
当手动调整 parent 大小时,我需要调整 child window 的大小。
为此,我在 parent window 上编写了以下代码:
SizeChanged += (sender, e) =>
{
if (e.PreviousSize.Width != 0 && e.PreviousSize.Height != 0)
{
m_RatioX *= e.NewSize.Width / e.PreviousSize.Width;
m_RatioY *= e.NewSize.Height / e.PreviousSize.Height;
}
WPFWindow p = ((WPFWindow)sender);
foreach (WPFWindow w in p.OwnedWindows)
{
if (w.m_doStretch)
{
// this work fine
w.Left *= m_RatioX / w.m_RatioX;
w.Top *= m_RatioY / w.m_RatioY;
w.Canvas.RenderTransform = new ScaleTransform(m_RatioX, m_RatioY);
// but not for the size modification
w.Width *= m_RatioX / w.m_RatioX;
w.Height *= m_RatioY / w.m_RatioY;
w.m_RatioX = m_RatioX;
w.m_RatioY = m_RatioY;
}
}
};
行
w.Width *= m_RatioX / w.m_RatioX;
w.Height *= m_RatioY / w.m_RatioY;
在它们对 child window 尺寸完全没有影响的意义上不起作用。
经过一些研究,我也尝试了以下两种选择,但第一种对 window 大小也没有影响,而第二种会因 NullReferenceException
.
崩溃
// first alternative
Window.GetWindow(w).Width = Window.GetWindow(w).Width * m_RatioX / w.m_RatioX;
Window.GetWindow(w).Height = Window.GetWindow(w).Height * m_RatioY / w.m_RatioY;
// second one
Application.Current.MainWindow = w;
Application.Current.MainWindow.Width = Application.Current.MainWindow.Width * m_RatioX / w.m_RatioX;
Application.Current.MainWindow.Height = Application.Current.MainWindow.Height * m_RatioY / w.m_RatioY;
那么,我应该如何修改 child window 的大小,以便有效调整其渲染大小?
编辑:
一些精度:
SizeChanged
处理程序仅在 parent window 中调用。
m_RatioX
和 m_RatioY
默认值为 1。
好的,我找到了解决方案。
不应直接使用 Width/Height 属性,而应使用相关的 DependencyProperty
。
即,使用 w.SetValue(Window.WidthProperty, <value>);
而不是 w.Width = <value>
.
当手动调整 parent 大小时,我需要调整 child window 的大小。 为此,我在 parent window 上编写了以下代码:
SizeChanged += (sender, e) =>
{
if (e.PreviousSize.Width != 0 && e.PreviousSize.Height != 0)
{
m_RatioX *= e.NewSize.Width / e.PreviousSize.Width;
m_RatioY *= e.NewSize.Height / e.PreviousSize.Height;
}
WPFWindow p = ((WPFWindow)sender);
foreach (WPFWindow w in p.OwnedWindows)
{
if (w.m_doStretch)
{
// this work fine
w.Left *= m_RatioX / w.m_RatioX;
w.Top *= m_RatioY / w.m_RatioY;
w.Canvas.RenderTransform = new ScaleTransform(m_RatioX, m_RatioY);
// but not for the size modification
w.Width *= m_RatioX / w.m_RatioX;
w.Height *= m_RatioY / w.m_RatioY;
w.m_RatioX = m_RatioX;
w.m_RatioY = m_RatioY;
}
}
};
行
w.Width *= m_RatioX / w.m_RatioX;
w.Height *= m_RatioY / w.m_RatioY;
在它们对 child window 尺寸完全没有影响的意义上不起作用。
经过一些研究,我也尝试了以下两种选择,但第一种对 window 大小也没有影响,而第二种会因 NullReferenceException
.
// first alternative
Window.GetWindow(w).Width = Window.GetWindow(w).Width * m_RatioX / w.m_RatioX;
Window.GetWindow(w).Height = Window.GetWindow(w).Height * m_RatioY / w.m_RatioY;
// second one
Application.Current.MainWindow = w;
Application.Current.MainWindow.Width = Application.Current.MainWindow.Width * m_RatioX / w.m_RatioX;
Application.Current.MainWindow.Height = Application.Current.MainWindow.Height * m_RatioY / w.m_RatioY;
那么,我应该如何修改 child window 的大小,以便有效调整其渲染大小?
编辑:
一些精度:
SizeChanged
处理程序仅在 parent window 中调用。m_RatioX
和m_RatioY
默认值为 1。
好的,我找到了解决方案。
不应直接使用 Width/Height 属性,而应使用相关的 DependencyProperty
。
即,使用 w.SetValue(Window.WidthProperty, <value>);
而不是 w.Width = <value>
.