【问题标题】:WPF MeasureOverride is not called on custom panel resize (Width => MinHeight)WPF MeasureOverride 不会在自定义面板调整大小时调用(宽度 => 最小高度)
【发布时间】:2012-10-22 11:44:15
【问题描述】:

我已经制作了自定义布局面板。我已经实现了 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。 Height 属性始终为 NaN,而 ActualHeight 发生变化,但不会引起测量。

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

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

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

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

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

【问题讨论】:

  • 您好,可以添加面板的代码吗?
  • 不确定“当我的面板调整大小时”是什么意思。除非我遗漏了某些东西,否则由于您在 ScrollViewer 中,因此您总是会被无限测量。如果您想使用实际可用大小进行测量并在 ScrollViewer 内,那么您必须是 ScrollViewer 的直接子级(而不是像您在 sn-p 中那样在 Border 内)并且您必须处理滚动(即实现IScrollInfo 并将 ScrollViewer 上的 CanContentScroll 设置为 true)。

标签: wpf layout


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-04-14
    • 2017-12-08
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2011-05-21
    • 1970-01-01
    相关资源
    最近更新 更多