Я хотел бы иметь возможность вставлять текст непосредственно в элемент управления RichTextBox, сохраняя вкладки, переводы строк и новые строки.
К сожалению, при вставке текста из буфера обмена (Ctrl-V) символы '\ r' помещаются в свойство Text элемента Run, а не переводятся в элемент <LineBreak />
.
Итак, учитывая вставленный текст:
Et harum quidem rerum facilis est et
Expandita Distinctio.
Nam libero tempore, cum soluta nobis
Есть Eligendi Optio Cumque Nihil
Impedit Quo минус ID Максимальное значение
placeat facere possimus, omnis
волюты предполагаем, омнис долор
repellendus.
и ...
<RichTextBox x:Name="rtbNoteText" BorderBrush="Gray" xml:space="preserve" AcceptsReturn="True" TextWrapping="Wrap"
BorderThickness="1" HorizontalAlignment="Stretch" Grid.Row="1"
VerticalAlignment="Stretch" Margin="30,0,30,30" FontSize="14"
VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" />
private void btnSaveNote_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(rtbNoteText.Xaml)) {
string contents = rtbNoteText.Xaml;
}
}
содержимое rtbNoteText.Xaml:
<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">
<Paragraph FontSize=\"14\" FontFamily=\"Portable User Interface\" Foreground=\"#FF000000\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\">
<Run Text=\"Et harum quidem rerum facilis est et expedita distinctio. \r\rNam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.\" />
</Paragraph>
</Section>
rtbNoteText.Replace("\r","<LineBreak />")
не сработает, потому что переводы строк встроены в текстовое свойство элемента Run.
Есть ли стандартный подход к этому вместо этого?:
<Section>
<Paragragh>
<Run>...</Run>
<LineBreak /><LineBreak />
<Run>...</Run>
</Paragraph>
</Section>