自定义面板调整大小时不调用WPF MeasureOverride(宽度=> MinHeight)

时间:2012-11-01 20:06:26

标签: wpf layout

我制作了自定义布局面板。我已经实现了MeasureOverride和ArrangeOverride方法。

以下是我使用我的面板的方法。

<Window
    x:Class="TestWindow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:TestPanel="clr-namespace:TestPanel;assembly=TestPanel"
    Title="MainWindow"
    Width="706"
    Height="200">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Height" Value="40" />
            <Setter Property="MinWidth" Value="120" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </Window.Resources>
    <ScrollViewer>
        <Border BorderBrush="Red" BorderThickness="1">
            <TestPanel:ShrinkPanel Background="Aqua" MaxColumns="4">
                <Button Content="Text 1" />
                <Button Content="Text 2" />
                <Button Content="Text 3" Height="75" />
                <Button Content="Text 4" />
                <Button Content="Text 5" />
                <Button Content="Text 6" Height="100" />
                <Button Content="Text 7" />
                <Button Content="Text 8" />
                <Button Content="Text 9" Height="75" />
            </TestPanel:ShrinkPanel>
        </Border>
    </ScrollViewer>
</Window>

一切正常,但调整面板大小时不会调用MeasureOverride。高度属性始终为NaN,而ActualHeight更改,但不会导致测量。

如何在调整大小时强制测量?

我订阅了SizeChanged事件并像这样调用了MeasureOverride

private void This_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Measure(e.NewSize);
}

但是这个代码似乎没有在框架中的任何地方使用。我认为根据宽度指定最小允许高度应该是一项非常常见的任务。

那么,重新测量并在调整大小时设置DesiredSize(或MinHeight / MinWidth?!)的正确方法是什么。

1 个答案:

答案 0 :(得分:2)

问题是滚动查看器:它接收滚动查看器,它为包含的项目提供无限高度/宽度,因此面板的所需大小为无限。您可以尝试禁用滚动查看器的垂直条,或者在自定义面板中特别处理所需大小为无穷大时的情况。 希望这对你有所帮助。