简体   繁体   English

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

[英]ScrollViewer does not scroll after adding textbox by code behind

I have problem with having ScrollViewer working. 我在使用ScrollViewer时遇到问题。

This is my MainWindow XAML: 这是我的MainWindow 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>

In code behind, I'm adding TextBox under LogoLabel and then I'm updating ScrollViewer's Layout like this: 在后面的代码中,我在LogoLabel下添加了TextBox,然后像这样更新ScrollViewer的Layout:

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

It looks like this: 看起来像这样:

似乎ScrollViewer根本不滚动

As you can see on screenshoot uploaded above, I don't know how to make ScrollViewer to scroll that I could see entire LogoTextBox height. 如您在上面上传的屏幕截图上所看到的,我不知道如何使ScrollViewer滚动才能看到整个LogoTextBox高度。 How to do it ? 怎么做 ? Any suggestions ? 有什么建议么 ?

You should scroll to a point outside of visible bounds. 您应该滚动到可见范围之外的点。 The Scroll.ScrollToVerticalOffset(nameTxt.Margin.Top) is already visible so no need to scroll. Scroll.ScrollToVerticalOffset(nameTxt.Margin.Top)已经可见,因此无需滚动。

You can try something like this: 您可以尝试如下操作:

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

By doing this the scroll control will scroll to show the full text box. 这样,滚动控件将滚动以显示完整的文本框。

The problem is with mainGrid height. 问题在于mainGrid高度。 It was set to auto. 它设置为自动。 I changed it to static and in code I increase it's height by nameTxt height. 我将其更改为静态,并在代码中通过nameTxt height增加了它的高度。 And it works. 而且有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM