wpf的富文本编辑器
时间: 2023-12-25 17:04:35 浏览: 212
以下是一个使用WPF中的RichTextBox控件自定义的文本编辑器的例子,具备文本字体、字体大小、颜色、背景色、加粗、斜线等基本功能:
```xaml
<Window x:Class="WpfRichTextBoxDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF RichTextBox Demo" Height="350" Width="525">
<Grid>
<RichTextBox x:Name="rtbEditor" Margin="10" VerticalScrollBarVisibility="Auto">
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Cut" Command="ApplicationCommands.Cut"/>
<MenuItem Header="Copy" Command="ApplicationCommands.Copy"/>
<MenuItem Header="Paste" Command="ApplicationCommands.Paste"/>
<Separator/>
<MenuItem Header="Bold" Command="Bold"/>
<MenuItem Header="Italic" Command="Italic"/>
<MenuItem Header="Underline" Command="Underline"/>
<Separator/>
<MenuItem Header="Font Family">
<MenuItem Header="Arial" Command="FontFamily" CommandParameter="Arial"/>
<MenuItem Header="Courier New" Command="FontFamily" CommandParameter="Courier New"/>
<MenuItem Header="Times New Roman" Command="FontFamily" CommandParameter="Times New Roman"/>
</MenuItem>
<MenuItem Header="Font Size">
<MenuItem Header="8" Command="FontSize" CommandParameter="8"/>
<MenuItem Header="12" Command="FontSize" CommandParameter="12"/>
<MenuItem Header="16" Command="FontSize" CommandParameter="16"/>
<MenuItem Header="20" Command="FontSize" CommandParameter="20"/>
<MenuItem Header="24" Command="FontSize" CommandParameter="24"/>
</MenuItem>
<Separator/>
<MenuItem Header="Font Color">
<MenuItem Header="Black" Command="TextColor" CommandParameter="Black"/>
<MenuItem Header="Red" Command="TextColor" CommandParameter="Red"/>
<MenuItem Header="Green" Command="TextColor" CommandParameter="Green"/>
<MenuItem Header="Blue" Command="TextColor" CommandParameter="Blue"/>
</MenuItem>
<MenuItem Header="Background Color">
<MenuItem Header="White" Command="TextBackground" CommandParameter="White"/>
<MenuItem Header="Yellow" Command="TextBackground" CommandParameter="Yellow"/>
<MenuItem Header="LightGray" Command="TextBackground" CommandParameter="LightGray"/>
<MenuItem Header="LightBlue" Command="TextBackground" CommandParameter="LightBlue"/>
</MenuItem>
</ContextMenu>
</RichTextBox.ContextMenu>
</RichTextBox>
</Grid>
</Window>
```
阅读全文