具有动态高度和下方菜单的滚动查看器

Scrollviewer with dynamic height and menu below

我找不到任何示例如何实现包含表单的滚动查看器,其中 save/cancel 按钮直接位于下方并且在 window here is a simple layout. I need to support resizing the window. I have many different lengths of forms, I want the scrollviewer to be as small as possible, and fill available space only if needed (if the form is larger). I don't want the buttons to always be pushed to the bottom of the window Not like this!, but to hug closer to the form (in the case of a shorter form) Something like this 的底部没有分开。

在我的网格中,我有一行用于 ScrollViewer,一行用于按钮,还有一行用于填充剩余的 space(如果有)。

我了解设置行高=Auto 会终止 ScrollViewer。那我该如何实现呢?这甚至可能吗?

Jeff -> 这是一个 image showing the window height increased. And an image showing the window height decreased,按钮可能会从视图中消失,并且滚动查看器会溢出,因此您无法看到所有内容

<Window x:Class="WpfAppRenderPathGeometry.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfAppRenderPathGeometry"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" x:Name="ScrollRow"/>
            <RowDefinition Height="80" x:Name="ButtonsRow"/>
            <RowDefinition Height="*" x:Name="SpaceRow"/>
        </Grid.RowDefinitions>
        
        <ScrollViewer x:Name="MyScroll" VerticalScrollBarVisibility="Visible">
            <Grid x:Name="MyForm">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="1" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="1" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="2" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="2" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="3" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="3" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="4" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="4" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="5" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="5" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="6" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="6" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="7" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="7" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="8" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="8" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="9" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="9" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="10" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="10" Grid.Column="1"></TextBox>

                <TextBlock Grid.Row="11" Grid.Column="0">LABEL</TextBlock>
                <TextBox Grid.Row="11" Grid.Column="1"></TextBox>
            </Grid>
        </ScrollViewer>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="1">
            <Button>CANCEL</Button>
            <Button>SAVE</Button>
        </StackPanel>
    </Grid>
</Window>


举个例子,我认为这会做你想做的事 - 只需根据需要进行调整。

我重新调整了,但我不确定我是否理解你的意图。

<Window
        x:Class="WpfSubDivideWindow.ScrollViewWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WpfSubDivideWindow"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:MyNamespace="clr-namespace:WpfSubDivideWindow"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="ScrollViewWindow"
        Width="800"
        Height="350"
        mc:Ignorable="d"
        >

    <Window.Resources>

        <Style x:Key="tbk.Base"
                TargetType="TextBlock"
                >
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontSize" Value="18" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0,12,20,12" />
        </Style>

        <Style x:Key="tbx.Base"
                TargetType="TextBox"
                >
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontSize" Value="18" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0,12,60,12" />
        </Style>

        <Style x:Key="Btn.Base"
                TargetType="Button"
                >
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontSize" Value="18" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Background" Value="DarkGreen" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Margin" Value="20,0,0,0" />
            <Setter Property="Height" Value="36" />
            <Setter Property="Width" Value="90" />
        </Style>

    </Window.Resources>

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition x:Name="ScrollRow" MaxHeight="{Binding ElementName=MyForm, Path=ActualHeight}" />
            <RowDefinition x:Name="ButtonsRow" Height="80" />
            <RowDefinition x:Name="SpaceRow" Height="Auto" />
        </Grid.RowDefinitions>

        <ScrollViewer x:Name="MyScroll"
                HorizontalScrollBarVisibility="Auto"
                VerticalScrollBarVisibility="Visible"
                >
            <Grid x:Name="MyForm"
                    Background="Moccasin"
                    >
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />

                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="160" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <TextBlock
                        Grid.Column="0"
                        Grid.Row="0"
                        Text="INDEX"
                        Style="{StaticResource tbk.Base}"
                        />


                <TextBox
                        Grid.Column="1"
                        Grid.Row="0"
                        Style="{StaticResource tbx.Base}"
                        />

                <TextBlock
                        Grid.Column="0"
                        Grid.Row="1"
                        Text="INDEX"
                        Style="{StaticResource tbk.Base}"
                        />


                <TextBox
                        Grid.Column="1"
                        Grid.Row="1"
                        Style="{StaticResource tbx.Base}"
                        />


                <TextBlock
                        Grid.Column="0"
                        Grid.Row="2"
                        Text="INDEX"
                        Style="{StaticResource tbk.Base}"
                        />


                <TextBox
                        Grid.Column="1"
                        Grid.Row="2"
                        Style="{StaticResource tbx.Base}"
                        />


                <TextBlock
                        Grid.Column="0"
                        Grid.Row="3"
                        Text="INDEX"
                        Style="{StaticResource tbk.Base}"
                        />


                <TextBox
                        Grid.Column="1"
                        Grid.Row="3"
                        Style="{StaticResource tbx.Base}"
                        />


                <TextBlock
                        Grid.Column="0"
                        Grid.Row="4"
                        Text="INDEX"
                        Style="{StaticResource tbk.Base}"
                        />


                <TextBox
                        Grid.Column="1"
                        Grid.Row="4"
                        Style="{StaticResource tbx.Base}"
                        />


                <TextBlock
                        Grid.Column="0"
                        Grid.Row="5"
                        Text="INDEX"
                        Style="{StaticResource tbk.Base}"
                        />


                <TextBox
                        Grid.Column="1"
                        Grid.Row="5"
                        Style="{StaticResource tbx.Base}"
                        />


                <TextBlock
                        Grid.Column="0"
                        Grid.Row="6"
                        Text="INDEX"
                        Style="{StaticResource tbk.Base}"
                        />


                <TextBox
                        Grid.Column="1"
                        Grid.Row="6"
                        Style="{StaticResource tbx.Base}"
                        />


                <TextBlock
                        Grid.Column="0"
                        Grid.Row="7"
                        Text="INDEX"
                        Style="{StaticResource tbk.Base}"
                        />


                <TextBox
                        Grid.Column="1"
                        Grid.Row="7"
                        Style="{StaticResource tbx.Base}"
                        />

            </Grid>
        </ScrollViewer>
        <StackPanel
                Grid.Row="1"
                HorizontalAlignment="Center"
                Orientation="Horizontal"
                >
            <Button
                    Content="Cancel"
                    Margin="0"
                    Style="{StaticResource Btn.Base}"
                    />
            <Button
                    Content="Save"
                    Style="{StaticResource Btn.Base}"
                    />

        </StackPanel>
    </Grid>
</Window>