我想将文本框中的文本打印到richtextboxes中,并在覆盖网格时出现滚动条。当您同时使用richtextbox和scrollviewer时,文本中的字母会逐行写入。当我删除滚动查看器时,文本看起来正常,但是当网格太多时,滚动不会出现并且超出了网格的边界。我该如何预防? Xaml:
<Grid>
<ScrollViewer CanContentScroll="False" Name="ChatScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel Name="Panel" />
</ScrollViewer>
<telerik:RadWatermarkTextBox Name="InputTextBox" VerticalAlignment="Bottom" Margin="10,0,97,60" Height="26" TextAlignment="Justify" />
<Button Content=">" Name="Send" Click="Send_Click" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,12,60" Width="80" Height="26"/>
</Grid>
Xaml.cs:
private void Send_Click(object sender, RoutedEventArgs e)
{
ChatPanel.Children.Add(CreateNewGrid(InputTextBox.Text));
ChatScrollViewer.ScrollToBottom();
InputTextBox.Clear();
}
private Grid CreateNewGrid(string message)
{
Grid newGrid = new Grid();
RichTextBox richTextBox = new RichTextBox();
richTextBox.FlowDirection = FlowDirection.LeftToRight;
richTextBox.Height = 80;
richTextBox.Padding = new Thickness(2);
richTextBox.HorizontalAlignment = HorizontalAlignment.Stretch;
richTextBox.Margin = new Thickness(40, 10, 100, 0);
richTextBox.Resources.Add(typeof(Paragraph), new Style(typeof(Paragraph)) { Setters = { new Setter(MarginProperty, new Thickness(0)) } });
Grid.SetColumn(richTextBoxTo, 1);
newGrid.Children.Add(richTextBox);
richTextBox.SetRichTBoxText(message);
return newGrid;
}
错误图片:
我水平对齐了 stackpanel,但是 Richtextbox 变小了,错误没有解决。 我用包裹面板尝试过,但效果不一样。
您没有提供相关代码的全部部分。通常,当流程文档
MinPageWidth
属性未设置时,可能会出现问题。
不管怎样,RichTextBox已经支持滚动了。因此,您可以尝试使用如下代码所示的内容:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<RichTextBox x:Name="rtb" Padding="2" FontSize="14" HorizontalAlignment="Left"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto" />
<!--<telerik:RadWatermarkTextBox Name="InputTextBox" VerticalAlignment="Bottom" Margin="10,0,97,60" Height="26" TextAlignment="Justify" />-->
<TextBox Grid.Row="1" x:Name="InputTextBox" MinHeight="40" AcceptsReturn="True"></TextBox>
<Button Grid.Row="2" Content=">" Name="Send" Click="Send_Click" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="80" Height="26"/>
</Grid>
背后的代码:
private void Send_Click(object sender, RoutedEventArgs e)
{
rtb.Document.Blocks.Add(AppentNewText(InputTextBox.Text));
rtb.ScrollToEnd();
InputTextBox.Clear();
}
private Paragraph AppentNewText(string message)
{
var p = new Paragraph() { Padding = new Thickness(1), Margin = new Thickness(1) };
p.Inlines.Add(new Run(message));
return p;
}