按后面的代码添加文本框后,ScrollViewer不会滚动

时间:2017-11-07 07:29:23

标签: c# wpf scrollviewer

让ScrollViewer工作时遇到问题。

这是我的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>

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

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

看起来像这样:

It seems that ScrollViewer does not scroll at all

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

2 个答案:

答案 0 :(得分:1)

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

您可以尝试这样的事情:

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

通过这样做,滚动控件将滚动显示全文框。

答案 1 :(得分:0)

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