我有一个绑定到菜单项的richtextbox,允许粗体、斜体和下划线以及剪切、复制和粘贴。但是,如何将richtextbox内容从后面的代码转换为字符串,从而携带粗体、斜体等?
</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" />
在后面的代码中,我试图做到这一点(不知道如何做到这一点),但文本块只是出来作为正常文本它不包含粗体等。
private void button1_Click(object sender, RoutedEventArgs e)
{
string myText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
textBlock1.Text = myText;
2条答案
按热度按时间uwopmtnx1#
你的问题的简单答案是:您不能。字符串不包含格式信息。您需要以支持文本格式的格式(如RTF)存储
RichTextBox
的内容。ipakzgxi2#
给您: