简体   繁体   English

为什么我的 RichTextBox 在我键入时不更改文本?

[英]Why doesn't my RichTextBox change the text when I type it?

I have a RichTextbox and now I am typing some text.我有一个 RichTextbox,现在我正在输入一些文本。 The text is also changed in the view, but if I now want to get to the text in the code behind, it always gives me the placeholder text and not the one I entered.视图中的文本也发生了变化,但如果我现在想查看后面代码中的文本,它总是给我占位符文本,而不是我输入的文本。

Which means that first look(PlaceHolder):这意味着第一眼(PlaceHolder):

Placeholder占位符

Code from The RichTextBox: 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>

Then my change in the View:然后我在视图中的更改:

ViewChange查看更改

And then I try to get the Text From Code behind:然后我尝试从后面的代码中获取文本:

string richText = new TextRange(rtb1.Document.ContentStart, rtb1.Document.ContentEnd).Text;
MessageBox.Show(richText);

But the Output is this:但输出是这样的:

Output输出

How Can I get that what I have written in Code Behind?我怎样才能得到我在后面的代码中写的内容?

I adjusted your script a bit by removing the <FlowDocument/> and replacing it with manual Width and Height.我通过删除<FlowDocument/>并将其替换为手动宽度和高度来稍微调整了您的脚本。

Try this:尝试这个:

<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>

And I added a try-catch to the script so you can debug easier:我在脚本中添加了一个 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);
}

It works for me这个对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM