【发布时间】:2011-07-10 20:21:39
【问题描述】:
我有一个 WPF 窗口,其中包含一个具有 MinWidth 和 MinHeight 的 UserControl。如何防止用户将 Window 的大小调整到违反 UserControl 的最小尺寸的程度?
这是我正在开发的 Window 的简化版本。我的真实应用的 UserControl 在这里被边框替换:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
<Button Content="OK"/>
<Button Content="Cancel"/>
</StackPanel>
<Border BorderBrush="Green" BorderThickness="10"
MinWidth="200" MinHeight="150"/>
</DockPanel>
</Window>
如果我将窗口缩小到足够小,边框的右边缘和下边缘就会被切断。我想防止窗口变得那么小——我希望我的窗口的最小尺寸正好是边框处于 its 最小尺寸的那个点。一些框架(如 Delphi VCL)自动将子控件的最小尺寸聚合到窗口;我希望 WPF 也能做到这一点,但显然事实并非如此。
我总是可以显式设置 Window 的 MinWidth 和 MinHeight,但要正确计算它们,我必须考虑 Button 的 ActualHeight,这意味着至少要等待一个布局通道(或手动调用 Measure)。凌乱。
有没有更好的方法来防止 Window 对其内容调整得太小?
【问题讨论】: