Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ScrollViewer在通过代码隐藏添加文本框后不会滚动_C#_Wpf_Scrollviewer - Fatal编程技术网

C# ScrollViewer在通过代码隐藏添加文本框后不会滚动

C# ScrollViewer在通过代码隐藏添加文本框后不会滚动,c#,wpf,scrollviewer,C#,Wpf,Scrollviewer,我无法让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/expres

我无法让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>
看起来是这样的:


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

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

您可以尝试以下方法:

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

执行此操作后,滚动控件将滚动显示全文框。

问题在于主栅格高度。它被设置为自动。我把它改成了静态的,在代码中我把它的高度增加了一倍。而且很有效。

是的。它应该可以工作,但它似乎是先滚动然后添加文本框。实际上,我可以将这个答案标记为解决方案。谢谢。为什么您有VerticalScrollBarVisibility=Hidden?应将其设置为自动,以便在内容大于高度时显示滚动条请将您的解决方案添加为答案,而不是对问题进行编辑以了解更多信息请参见