我想通过两种方式更改 Richtextbox 中的格式:
我的第二个要求的示例:我的 Richtextbox 获得了文本
堆栈溢出
并且插入符号光标位于 stackover 和 flow 之间,如下所示:
叠加|流程
现在我需要将当前插入符号位置的字体大小更改为 20。在进一步编写时,我只想让该位置上的文本显示得更大。我不想更改当前现有的文本。喜欢:
叠加酷流程
cool 现在的字体大小应为 20,并且 stackover flow 采用默认字体大小(像以前一样)
我找到了很多答案,但没有一个对我有帮助。
大多数提示是:
但它们对我来说都不起作用......
我主要关注以下问题wpf-richtextbox-fontface-fontsize
目前我最终得到了以下代码,但它只改变了文本选择。
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Orientation="Vertical">
<RichTextBox Height="300" Width="300" x:Name="MyRichTextBox" />
<ComboBox Width="300" x:Name="cmbbx" SelectedIndex="1" SelectionChanged="ComboBox_SelectionChanged" />
</StackPanel>
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace WpfApp2
{
public partial class MainWindow : Window
{
public ObservableCollection<double> Werte { get; set; }
public MainWindow()
{
Werte = new ObservableCollection<double>();
Werte.Add(4);
Werte.Add(10);
Werte.Add(20);
Werte.Add(55);
InitializeComponent();
cmbbx.ItemsSource = Werte;
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var newSize = (double)cmbbx.SelectedValue;
TextRange r = new TextRange(MyRichTextBox.Selection.Start, MyRichTextBox.Selection.End);
r.ApplyPropertyValue(TextElement.FontSizeProperty, newSize);
}
}
}
这只是改变文本选择。当没有选择任何内容时(仅设置光标),没有格式设置。我正在使用 .net 5
可能您想要如下所示的东西:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ComboBox Grid.Row="0" Width="60" x:Name="cmbbx" SelectedIndex="1" SelectionChanged="ComboBox_SelectionChanged" HorizontalAlignment="Left"/>
<RichTextBox Grid.Row="1" x:Name="rtb" AllowDrop="True" VerticalScrollBarVisibility="Auto" Padding="2"
SelectionChanged="rtb_SelectionChanged"
TextChanged="rtb_TextChanged">
<FlowDocument/>
</RichTextBox>
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Collections.ObjectModel;
namespace WpfApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
var Werte = new ObservableCollection<double> { 4, 10, 16, 20, 26, 55 };
InitializeComponent();
cmbbx.ItemsSource = Werte;
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!rtb.Selection.IsEmpty)
{
if (cmbbx.SelectedValue is double newSize)
{
var range = new TextRange(rtb.Selection.Start, rtb.Selection.End);
range.ApplyPropertyValue(TextElement.FontSizeProperty, newSize);
}
}
}
// Use `TextChanged` event to apply the currently selected font size to the entered text
private void rtb_TextChanged(object sender, TextChangedEventArgs e)
{
// Disable recursive calling this event handler
rtb.TextChanged -= rtb_TextChanged;
foreach (var change in e.Changes)
{
if (cmbbx != null)
{
TextPointer tp = rtb.Document.ContentStart.GetPositionAtOffset(change.Offset);
var range = new TextRange(tp, tp.GetPositionAtOffset(1, LogicalDirection.Backward));
if (!string.IsNullOrEmpty(range.Text) && cmbbx.SelectedValue is double fontsize)
{
range.ApplyPropertyValue(TextElement.FontSizeProperty, fontsize);
}
}
}
// Restore the event handler
rtb.TextChanged += rtb_TextChanged;
}
// The following `SelectionChanged` event handler is used to set a font size in the combobox
// depend on the current caret location.
private void rtb_SelectionChanged(object sender, RoutedEventArgs e)
{
if (rtb.Selection is TextSelection selection)
{
if (selection.GetPropertyValue(TextElement.FontSizeProperty) is double fsize)
{
foreach (var it in cmbbx.Items)
{
if ((double)it == fsize)
{
cmbbx.SelectedItem = it;
return;
}
}
}
cmbbx.SelectedItem = null;
}
}
}
}