4

出于某种奇怪的原因,在我添加了大量文本后,嵌入在滚动查看器中的 WPF 文本框突然被切断,如下图所示。是否有一些限制或达到了我可以做大的东西?

在此处输入图像描述

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

这是相关的 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();
            });
4

1 回答 1

3

第二次更新:

好的,所以我决定获取 windows phone 套件并尝试一下。

TextBox正如OP提到的那样,它不会滚动。因此我决定看看它的默认值ControlTemplate

这是ControlTemplate从带有 windows phone 8 sdk 的 vs2012 中剥离出来的TextBox

<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 验证了这一点。

不确定为什么它没有 abt 的原因,但在解决问题时ScrollViewer添加一个。ControlTemplate

解决方案:

Full Stylefor TextBoxwith 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>

请注意只是将整个控件包装TextBoxScrollViewer滚动的整个控件中,而不仅仅是其中的内容,这对于 UX POV 来说可能不是很吸引人

下载带有上述修复的示例项目链接:DropBox

于 2013-06-01T11:51:32.443 回答