我有一個 RichTextbox,現在我正在輸入一些文本。視圖中的文本也發生了變化,但如果我現在想查看后面代碼中的文本,它總是給我占位符文本,而不是我輸入的文本。
這意味著第一眼(PlaceHolder):
占位符
RichTextBox 中的代碼:
<Grid Grid.Row="2">
<RichTextBox x:Name="rtb1" IsHitTestVisible="True" HorizontalAlignment="Right"
TextChanged="OnDocumentChanged" BorderThickness="0" FontSize="40"
Background="Transparent" Panel.ZIndex="2">
<FlowDocument TextAlignment="Center" >
<Paragraph >
<Run Text="Hallo" />
</Paragraph>
</FlowDocument>
</RichTextBox>
<TextBox x:Name="tb1" Style="{StaticResource tb1}" Text="Eingabe" FontSize="40" TextWrapping="Wrap" Panel.ZIndex="1" TextAlignment="Center" Foreground="DarkGray">
</TextBox>
</Grid>
然后我在視圖中的更改:
查看更改
然后我嘗試從后面的代碼中獲取文本:
string richText = new TextRange(rtb1.Document.ContentStart, rtb1.Document.ContentEnd).Text;
MessageBox.Show(richText);
但輸出是這樣的:
輸出
我怎樣才能得到我在后面的代碼中寫的內容?
uj5u.com熱心網友回復:
<FlowDocument/>我通過洗掉并用手動寬度和高度替換它來稍微調整你的腳本。
嘗試這個:
<Grid Grid.Row="2">
<RichTextBox x:Name="rtb1" IsHitTestVisible="True" HorizontalAlignment="Center"
TextChanged="rtb1_TextChanged" BorderThickness="0" FontSize="40"
Background="Transparent" Panel.ZIndex="2" Width="400" Height="100"
VerticalAlignment="Top" Cursor="Arrow"/>
<TextBox x:Name="tb1" Text="Eingabe" FontSize="40" TextWrapping="Wrap"
Panel.ZIndex="1" TextAlignment="Center" Foreground="DarkGray"/>
</Grid>
我在腳本中添加了一個 try-catch,以便您可以更輕松地除錯:
try
{
string richText = new TextRange(rtb1.Document.ContentStart, rtb1.Document.ContentEnd).Text;
Console.WriteLine(richText);
MessageBox.Show(richText);
} catch (System.Windows.Markup.XamlParseException ex)
{
Console.WriteLine(ex.StackTrace);
}
這個對我有用
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494713.html
上一篇:ExcelVBA中的洗掉線
