1

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

4

1 に答える 1

2

問題は Scroll Viewer です。Scroll Viewer が含まれるアイテムに無限の高さ/幅を与えているため、パネルの希望のサイズは無限大です。スクロール ビューアーの垂直バーを無効にするか、カスタム パネルで目的のサイズが無限大の場合を特に扱います。これがお役に立てば幸いです。

于 2012-11-01T20:33:53.507 に答える