richtextbox转换为字符串 [英] richtextbox to string

查看:89
本文介绍了richtextbox转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到菜单项的Richtextbox,允许粗体,斜体和下划线以及剪切复制和粘贴.但是,如何从后面的代码中获取richtextbox内容并将其转换为字符串,该代码将带有粗体,斜体等?

I have a richtextbox bound to a menu item to allow bold, italic and underline aswell as cut copy and paste. But how do you take the richtextbox content and turn it into a string from code behind which will carry the bold, italic etc?

        </DockPanel>
            <DockPanel Height="259" VerticalAlignment="Bottom">
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="Edit">
                    <MenuItem Command="Cut" Header="_Cut" />
                    <MenuItem Command="Copy" Header="C_opy" />
                    <MenuItem Command="Paste" Header="_Paste" />
                </MenuItem>
            </Menu>
            <ToolBarTray DockPanel.Dock="Top">
                <ToolBar>
                    <Button Command="Cut" Content="Cut" />
                    <Button Command="Copy" Content="Copy" />
                    <Button Command="Paste" Content="Paste" />
                            <ToggleButton MinWidth="40"
                    Command="EditingCommands.ToggleBold"
                    CommandTarget="{Binding ElementName=XAMLRichBox}"
                    TextBlock.FontWeight="Bold">B</ToggleButton>
                            <ToggleButton MinWidth="40"
                    Command="EditingCommands.ToggleItalic"
                    CommandTarget="{Binding ElementName=XAMLRichBox}"
                    TextBlock.FontStyle="Italic">I</ToggleButton>
                            <ToggleButton MinWidth="40"
                    Command="EditingCommands.ToggleUnderline"
                    CommandTarget="{Binding ElementName=XAMLRichBox}">
                                <TextBlock TextDecorations="Underline">U</TextBlock>
                            </ToggleButton>
                        </ToolBar>
            </ToolBarTray>
                <RichTextBox HorizontalAlignment="Left" Margin="0,0,0,0" Name="richTextBox1"  Height="211" VerticalAlignment="Top" Width="398" />

在后面的代码中,我尝试执行此操作(不确定如何执行),但是该文本块只是作为普通文本出现的,它不包含粗体字等.

In the code behind I was trying to do this (not sure on how to do it) but the textblock just comes out as normal text it doesnt contain the bold etc.

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        string myText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
        textBlock1.Text = myText;

推荐答案

您的问题的简单答案是:您不能.字符串不包含格式信息.您需要以支持文本格式的格式(例如RTF)存储RichTextBox的内容.

The simple answer to your question is: you can't. A string contains no formatting information. You'll need to store the RichTextBox's content in a format that supports text formatting, like RTF.

这篇关于richtextbox转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆