简体   繁体   中英

ScrollViewer does not scroll after adding textbox by code behind

I have problem with having ScrollViewer working.

This is my 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:

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. 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.

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. It was set to auto. I changed it to static and in code I increase it's height by nameTxt height. And it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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