【问题标题】:Having a problem with the Grid height inside scrollviewer WPF XAML滚动查看器 WPF XAML 中的网格高度有问题
【发布时间】:2022-08-18 18:43:48
【问题描述】:

我在滚动查看器中遇到网格高度问题。我正在将我的网格高度与滚动查看器绑定。如果我给它固定的高度,那么当我在不同的设备上运行应用程序时,它只会破坏网格的布局。通常,当我向其中添加更多内容时,网格高度应该会动态增加,但它只是固定的并且所有内容都在缩小。

下面我附上了我的部分代码。

Here is the image where the height is binded with the scrollviewer

In this image, I gave a fixed height 400 and it shows what I want

This is the image of application and it just does not have the same design as in the XAML Layout

<ScrollViewer x:Name=\"scrollviewer\" Grid.Column=\"4\" Grid.Row=\"7\" Grid.ColumnSpan=\"7\" Grid.RowSpan=\"20\" HorizontalScrollBarVisibility=\"Disabled\" VerticalScrollBarVisibility=\"Visible\" VerticalContentAlignment=\"Stretch\">

        <Grid x:Name=\"GridInputs\" Height=\"400\" VerticalAlignment=\"Stretch\">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width=\"0.5*\"/>
                <ColumnDefinition Width=\"0.5*\"/>
                <ColumnDefinition Width=\"0.4*\"/>
                <ColumnDefinition Width=\"0.1*\"/>
                <ColumnDefinition Width=\"0.1*\"/>
                <ColumnDefinition Width=\"0.4*\"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
                <RowDefinition Height=\"0.1*\"/>
                <RowDefinition Height=\"0.3*\"/>
            </Grid.RowDefinitions>
            <!-- Row 1 -->
            <Border Grid.Column=\"0\" Grid.Row=\"0\" BorderBrush=\"Black\" Grid.ColumnSpan=\"7\" BorderThickness=\"1\" CornerRadius=\"5\"/>
            <!--<Viewbox Grid.Row=\"6\"  Grid.Column=\"4\" Grid.ColumnSpan=\"3\" x:Name=\"vbLongest\">-->
            <Grid Grid.Row=\"0\"  Grid.Column=\"0\" Grid.ColumnSpan=\"3\">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width=\"0.2*\"/>
                    <ColumnDefinition Width=\"6*\"/>
                    <ColumnDefinition Width=\"1*\"/>
                    <ColumnDefinition Width=\"6*\"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height=\"0.1*\"/>
                    <RowDefinition Height=\"2*\"/>
                    <RowDefinition Height=\"0.1*\"/>
                </Grid.RowDefinitions>
                <Viewbox Grid.Column=\"1\" Grid.Row=\"1\" StretchDirection=\"DownOnly\" HorizontalAlignment=\"Left\">
                    <!--<TextBlock TextWrapping=\"Wrap\" Text=\"Ground height to AHP\" FontSize=\"5\" Foreground=\"Black\"/>-->
                    <Label Content=\"Ground Height to AHP Z\" FontSize=\"20\" FontWeight=\"Bold\" HorizontalContentAlignment=\"Left\" />
                </Viewbox>
                <Viewbox Grid.Column=\"3\" Grid.Row=\"1\" StretchDirection=\"DownOnly\">
                    <Label x:Name=\"GroundHeight\" HorizontalAlignment=\"Right\" Content=\"Ground height to AHP\" FontSize=\"20\" FontWeight=\"Bold\" Foreground=\"Gray\"/>
                </Viewbox>
            </Grid>
            <!--</Viewbox>-->
            <Viewbox Grid.Row=\"0\" Grid.Column=\"3\" Margin=\"2\">
                <TextBlock HorizontalAlignment=\"Center\" TextWrapping=\"Wrap\" Text=\"Z\" FontSize=\"20\" Foreground=\"Black\"/>
            </Viewbox>
            <Viewbox Grid.Row=\"0\" Grid.Column=\"4\" Margin=\"2\">
                <TextBlock HorizontalAlignment=\"Center\" TextWrapping=\"Wrap\" Text=\"=\" FontSize=\"20\" Foreground=\"Black\"/>
            </Viewbox>
            <TextBox x:Name=\"tbGroundZ\" Grid.Column=\"5\" Grid.Row=\"0\" Margin=\"2\" HorizontalContentAlignment=\"Center\" VerticalContentAlignment=\"Center\" Text=\"{Binding CurrentPackage[BuckHeightZ].Value, Mode=TwoWay, UpdateSourceTrigger=LostFocus}\"
                         FontSize=\"{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Converter={StaticResource fontSizeCon}}\"/>
</Grid>
  • 您是否尝试过不设置网格的高度?我相信网格应该自动扩展以适应滚动查看器。
  • 是的,我也尝试过不设置高度,但它会破坏布局,然后每一行都超级大。
  • 在您的网格中将行定义设置为自动,然后添加一个带有 * 高度的额外行 - 它会占用剩余空间。
  • 啊,只是仔细查看了您的 XAML。我认为你不应该像你那样做行高。尝试做自动行高,或固定高度和*最后一行,就像我上面提到的那样。你知道 *x 是如何工作的吗?
  • 我刚试过你的方法还是不行。设计布局中断,有些行很大,有些行很小。行之间也没有间隙,我给出的高度为 0.1*。此外,我不是 XAML 专家,但我知道 * 是如何工作的。

标签: c# .net wpf xaml data-binding


【解决方案1】:

在滚动查看器之后添加视图框并将所有网格移动到视图框中,解决了我的问题。现在,我可以为网格提供任何固定的高度和宽度,并且不会破坏设计。

Viewbox 根据显示分辨率或尺寸拉伸和缩放子元素。

【讨论】:

    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多