wpf - 具有动态高度和下方菜单的 Scrollviewer
问题描述
我找不到任何示例如何实现包含表单的滚动查看器,保存/取消按钮直接位于下方,而不是在窗口底部分开,这是一个简单的布局。我需要支持调整窗口大小。我有许多不同长度的表单,我希望滚动查看器尽可能小,并且仅在需要时才填充可用空间(如果表单更大)。我不希望按钮总是被推到窗口的底部不是这样的!,但要更靠近表格(在较短的表格的情况下)这样的东西。
在我的网格中,有一行用于 ScrollViewer,一行用于按钮,还有一行用于填充剩余空间(如果有)。
我知道通过设置行高=自动杀死 ScrollViewer。那我该如何实现呢?这甚至可能吗?
Jeff -> 这是显示窗口高度增加的图像。并且显示窗口高度降低的图像,按钮可能会从视图中消失,并且滚动查看器溢出,因此您无法看到所有内容
<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>
推荐阅读
- google-cloud-platform - BigQuery:用于存储的单独项目和用于查询的项目
- javascript - 在由 dangerouslySetInnerHTML 设置的 HTML 中添加 React 组件
- javascript - 如果值的长度为 0,如何重置 fetch 调用?
- java - 迁移 spring boot 1.5 -> 2.2 后的 Spring Security 登录问题
- python - python单元测试没有找到另一个文件夹模块
- php - db 行未更新
- python - Keras 指标的“名称”有什么作用?
- angular - NgFor 和组件选择器在内部不起作用
- php - VS Code PHP自动格式化/美化Stroustrup风格的大括号(K&R变体)和括号中的空格?
- git - 在错误的 git 操作后恢复 myfile.py