カスタム レイアウト パネルを作成しました。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 ?!) を設定する正しい方法は何ですか?