2012-11-01 33 views
1

我制作了自定义布局面板。我已经实现了MeasureOverride和ArrangeOverride方法。WPF MeasureOverride不在自定义面板调整大小上调用(Width => MinHeight)

这是我如何使用我的面板。

<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 ?!)调整大小。

+0

你好,你可以添加面板的代码? –

+2

不确定您的意思是“当我的面板调整大小”。除非我错过了某些东西,否则你总是要用无穷大进行测量,因为你在ScrollViewer中。如果你想用实际可用的大小来衡量并且在ScrollViewer中,那么你必须是ScrollViewer的直接子元素(不像在你的代码片段中那样在边框内),你必须处理滚动(即实现IScrollInfo和将ScrollViewer上的CanContentScroll设置为true)。 – AndrewS

回答

2

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