通过后面的代码添加文本框后,ScrollViewer 不滚动

ScrollViewer does not scroll after adding textbox by code behind

我在使用 ScrollViewer 时遇到问题。

这是我的主窗口XAML:

<Window x:Class="Labels.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:Labels"
    mc:Ignorable="d"
    Title="Labels" Height="350" Width="250"  WindowStartupLocation="CenterScreen"
    ResizeMode="NoResize" Background="#FFF6A300">

<ScrollViewer VerticalScrollBarVisibility="Hidden" Name="Scroll">

    <Grid Margin="0,0,0,0" HorizontalAlignment="Center" Height="auto"
          VerticalAlignment="Center" Name="mainGrid">

        <Label x:Name="ProductLabel" Content="Product" HorizontalAlignment="Center"
               Width="200" FontSize="16"
               FontWeight="Bold" VerticalAlignment="Top"
               HorizontalContentAlignment="Center" Margin="15,-5,9,0"/>

        <TextBox x:Name="ProductTextBox" HorizontalAlignment="Center" Height="28"
                 Margin="13,22,11,0"
                 TextWrapping="Wrap" VerticalAlignment="Top" Width="200"
                 PreviewKeyDown="ProductTextBox_PreviewKeyDown"
                 SpellCheck.IsEnabled="True" FontSize="16"
                 HorizontalContentAlignment="Center"/>

        <Label x:Name="IndexLabel" Content="Index: " HorizontalAlignment="Left"
               Margin="10,50,0,0" VerticalAlignment="Top" Height="35" Width="62"/>

        <Label x:Name="NameLabel" Content="Name:" HorizontalAlignment="Left"
               Margin="10,80,0,0" VerticalAlignment="Top" Height="31" Width="62"/>

        <TextBlock x:Name="IndexTextBlock" HorizontalAlignment="Left"
                   Margin="58,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
                   Width="156" Height="23"/>

        <TextBlock x:Name="NameTextBlock" HorizontalAlignment="Left"
                   Margin="58,85,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
                   Width="155" Height="52"/>

        <Label x:Name="TypeLabel" Content="Label template:"
               HorizontalAlignment="Center" VerticalAlignment="Top" Height="30"
               Width="199" FontSize="16" FontWeight="Bold"
               Margin="15,142,10,0" HorizontalContentAlignment="Center"/>

        <TextBox x:Name="TypeTextBox" HorizontalAlignment="Center"
                 Height="29" Margin="15,172,9,0" TextWrapping="Wrap"
                 VerticalAlignment="Top" Width="200"
                 PreviewKeyDown="TypeTextBox_PreviewKeyDown" 
                 HorizontalContentAlignment="Center" FontSize="16"/>

        <Label x:Name="CountLabel" Content="Print Copies: " HorizontalAlignment="Center"
               Margin="14,206,10,0"
               VerticalAlignment="Top" Height="31" Width="200" FontSize="16"
               FontWeight="Bold" HorizontalContentAlignment="Center"/>

        <TextBox x:Name="CountTextBox"
                 PreviewTextInput="CountTextBox_PreviewTextInput"
                 HorizontalAlignment="Center"
                 Height="28" Margin="13,232,11,0" TextWrapping="Wrap"
                 VerticalAlignment="Top" Width="200"
                 PreviewKeyDown="CountTextBox_PreviewKeyDown"
                 HorizontalContentAlignment="Center" FontSize="16"/>

        <Label x:Name="LogoLabel" Content="Label" HorizontalAlignment="Left"
               Margin="93,268,0,-8" VerticalAlignment="Top" Visibility="Hidden"/>
    </Grid>
</ScrollViewer>

在后面的代码中,我在 LogoLabel 下添加了 TextBox,然后像这样更新了 ScrollViewer 的布局:

mainGrid.Children.Add(nameTxt);
LogoLabel.Visibility = Visibility.Visible;
CountTextBox.IsEnabled = false;
nameTxt.Focus();
Scroll.UpdateLayout();
Scroll.ScrollToVerticalOffset(nameTxt.Margin.Top);

看起来像这样:

如您在上面上传的屏幕截图中所见,我不知道如何使 ScrollViewer 滚动到我可以看到整个 LogoTextBox 高度。怎么做 ?有什么建议吗?

您应该滚动到可见范围之外的点。 Scroll.ScrollToVerticalOffset(nameTxt.Margin.Top) 已经可见,因此无需滚动。

您可以尝试这样的操作:

Scroll.ScrollToVerticalOffset(nameTxt.Margin.Top + nameTxt.Height);

通过执行此操作,滚动控件将滚动以显示完整的文本框。

问题在于 mainGrid 高度。它被设置为自动。我将其更改为静态,并在代码中将其高度增加 nameTxt 高度。并且有效。