WPF文本框被截断

4

由于我向WPF滚动查看器中嵌入了大量文本后,我的文本框会突然像下图一样被截断。是否有达到的限制或其他什么可以扩大或增加的地方?

enter image description here

我没有收到任何错误消息。

以下是相关的Xaml代码:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Button x:Name="Slice_Button" Content="Slice" HorizontalAlignment="Left" Margin="106,0,0,0" VerticalAlignment="Top" Click="Slice_Button_Click" Height="87" Background="#FF0D5B1E"/>
        <Button x:Name="CancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="232,0,0,0" VerticalAlignment="Top" Height="87" Click="Button_Click_1" Background="#FFC70E0E"/>
        <ScrollViewer x:Name="Scroller" HorizontalAlignment="Left" Height="505" Margin="10,92,0,0" VerticalAlignment="Top" Width="436">
            <TextBox x:Name="OutBox" TextWrapping="Wrap" Text="Output will be displayed here" IsReadOnly="True" Margin="2"/>
        </ScrollViewer>

    </Grid>

这里是我使用的C#代码来添加文本:

main.DispatchInvoke(() =>
            {
                main.OutBox.Text += newText;
                main.Scroller.ScrollToVerticalOffset(main.Scroller.ScrollableHeight);
                main.Scroller.UpdateLayout();
            });

1
老兄,请发布相关的XAML代码... - Federico Berasategui
抱歉,我忘记了,但现在我已经添加了。 - Gerharddc
从东西中删除“Height”和“Width”。WPF不需要它们。 - Federico Berasategui
那只是让我的文本框变小了,它仍然有完全相同的问题。 - Gerharddc
很抱歉如果这听起来很愚蠢,但我如何在没有滚动条的情况下使文本框滚动? - Gerharddc
显示剩余2条评论
1个回答

3

第二次更新:

好的,我决定获取Windows Phone开发工具包并尝试一下。

正如楼主所提到的,TextBox无法滚动。因此,我决定查看其默认的ControlTemplate

这是从VS2012和Windows Phone 8 SDK中剥离出来的用于TextBoxControlTemplate

<ControlTemplate TargetType="TextBox">
  <Grid Background="Transparent">
    <!--  VisualState Groups for abt 100 lines  -->
    <Border x:Name="MainBorder"
            Margin="{StaticResource PhoneTouchTargetOverhang}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}" />
    <Border x:Name="ReadonlyBorder"
            Margin="{StaticResource PhoneTouchTargetOverhang}"
            Background="Transparent"
            BorderBrush="{StaticResource PhoneDisabledBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            Visibility="Collapsed" />
    <Border Margin="{StaticResource PhoneTouchTargetOverhang}"
            Background="Transparent"
            BorderBrush="Transparent"
            BorderThickness="{TemplateBinding BorderThickness}">
      <ContentControl x:Name="ContentElement"
                      Margin="{StaticResource PhoneTextBoxInnerMargin}"
                      HorizontalContentAlignment="Stretch"
                      VerticalContentAlignment="Stretch"
                      BorderThickness="0"
                      Padding="{TemplateBinding Padding}" />
    </Border>
  </Grid>
</ControlTemplate>

没有ScrollViewer,只有一个ContentControl。以下内容来自vs2012桌面应用程序TextBox
<ControlTemplate TargetType="{x:Type TextBox}">
  <Border x:Name="border"
          Background="{TemplateBinding Background}"
          BorderBrush="{TemplateBinding BorderBrush}"
          BorderThickness="{TemplateBinding BorderThickness}"
          SnapsToDevicePixels="True">
    <ScrollViewer x:Name="PART_ContentHost"
                  Focusable="False"
                  HorizontalScrollBarVisibility="Hidden"
                  VerticalScrollBarVisibility="Hidden" />
  </Border>

将手机模板复制到桌面应用程序并遇到同样的问题,已与 Snoop 验证。

不确定为什么它没有 ScrollViewer,但在 ControlTemplate 中添加一个可以解决问题。

解决方案:

TextBox 的完整 Style,包含 ScrollViewer(在 "App.xaml" 中添加 -> <Application.Resources></Application.Resources>

<Style x:Key="TextBoxStyle1"
       TargetType="TextBox">
  <Setter Property="FontFamily"
          Value="{StaticResource PhoneFontFamilyNormal}" />
  <Setter Property="FontSize"
          Value="{StaticResource PhoneFontSizeMediumLarge}" />
  <Setter Property="Background"
          Value="{StaticResource PhoneTextBoxBrush}" />
  <Setter Property="Foreground"
          Value="{StaticResource PhoneTextBoxForegroundBrush}" />
  <Setter Property="BorderBrush"
          Value="{StaticResource PhoneTextBoxBrush}" />
  <Setter Property="SelectionBackground"
          Value="{StaticResource PhoneAccentBrush}" />
  <Setter Property="SelectionForeground"
          Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}" />
  <Setter Property="BorderThickness"
          Value="{StaticResource PhoneBorderThickness}" />
  <Setter Property="Padding"
          Value="2" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="TextBox">
        <Grid Background="Transparent">
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Normal" />
              <VisualState x:Name="MouseOver" />
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
                                                 Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="Transparent" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
                                                 Storyboard.TargetProperty="BorderBrush">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource PhoneDisabledBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement"
                                                 Storyboard.TargetProperty="Foreground">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource PhoneDisabledBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="ReadOnly">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
                                                 Storyboard.TargetProperty="Visibility">
                    <DiscreteObjectKeyFrame KeyTime="0">
                      <DiscreteObjectKeyFrame.Value>
                        <Visibility>Collapsed</Visibility>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ReadonlyBorder"
                                                 Storyboard.TargetProperty="Visibility">
                    <DiscreteObjectKeyFrame KeyTime="0">
                      <DiscreteObjectKeyFrame.Value>
                        <Visibility>Visible</Visibility>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ReadonlyBorder"
                                                 Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource PhoneTextBoxBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ReadonlyBorder"
                                                 Storyboard.TargetProperty="BorderBrush">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource PhoneTextBoxBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement"
                                                 Storyboard.TargetProperty="Foreground">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource PhoneTextBoxReadOnlyBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="FocusStates">
              <VisualState x:Name="Focused">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
                                                 Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource PhoneTextBoxEditBackgroundBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
                                                 Storyboard.TargetProperty="BorderBrush">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource PhoneTextBoxEditBorderBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Unfocused" />
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border x:Name="MainBorder"
                  Margin="{StaticResource PhoneTouchTargetOverhang}"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}" />
          <Border x:Name="ReadonlyBorder"
                  Margin="{StaticResource PhoneTouchTargetOverhang}"
                  Background="Transparent"
                  BorderBrush="{StaticResource PhoneDisabledBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Visibility="Collapsed" />
          <Border Margin="{StaticResource PhoneTouchTargetOverhang}"
                  Background="Transparent"
                  BorderBrush="Transparent"
                  BorderThickness="{TemplateBinding BorderThickness}">
            <ScrollViewer>
              <ContentControl x:Name="ContentElement"
                              Margin="{StaticResource PhoneTextBoxInnerMargin}"
                              HorizontalContentAlignment="Stretch"
                              VerticalContentAlignment="Stretch"
                              BorderThickness="0"
                              Padding="{TemplateBinding Padding}" />
            </ScrollViewer>
          </Border>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

使用方法:

<Grid x:Name="ContentPanel"
      Grid.Row="1"
      Margin="12,0,12,0">
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>
  <StackPanel Grid.Row="0"
              HorizontalAlignment="Center"
              Orientation="Horizontal">
    <Button x:Name="Slice_Button"
            Margin="10 0"
            Background="#FF0D5B1E"
            Content="Slice" />
    <Button x:Name="CancelButton"
            Margin="10 0"
            Background="#FFC70E0E"
            Content="Cancel" />
  </StackPanel>
  <TextBox x:Name="OutBox"
           Grid.Row="1"
           Margin="10"
           IsReadOnly="True"
           Style="{StaticResource TextBoxStyle1}"
           Text="Output will be displayed here"
           TextWrapping="Wrap" />
</Grid>

请注意,仅将TextBox包装在ScrollViewer中会滚动整个控件,而不仅是其中的内容,这可能对用户体验不太有吸引力。
上述修复的示例项目下载链接:DropBox

@Gerhman 噢,对不起那个。如果它有一个网格,甚至可能会用那个...我马上更新我的答案。 - Viv
感谢您编辑它,它现在已被接受,但它根本不滚动,无论是自动还是通过物理输入(拖动它)。 - Gerharddc
非常感谢您找出风格,它使用户界面简化了很多,但我仍然几乎有原来的问题。在停止写入文本并使滚动区域变得巨大之前,我会获得约1行以上的文本。您可以通过运行一个每隔几秒钟写入一行文本的方法来测试此功能。 - Gerharddc
抱歉,我没有理解你上一条评论的意思。我甚至不确定你想表达什么。如果我在MainPage构造函数中添加Loaded += async (sender, args) => { for (int i = 0; i < 100; ++i) { await Task.Delay(1000); OutBox.Text += Guid.NewGuid().ToString() + Environment.NewLine; } };,我可以看到TextBox被很好地填充了。 - Viv
发生的情况是,当文本框达到一定行数后,它就停止显示我添加的任何进一步的行。它会创建一个大的空白空间。 - Gerharddc
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接