RichTextBox.Selection 和 TextSelection.ApplyPropertyValue 的问题
Problems with RichTextBox.Selection and TextSelection.ApplyPropertyValue
我正在尝试使用 RichTextBox 在 WPF 中创建一个文本编辑器。
我的问题是更改文本的字体大小。我的代码在每种情况下都按预期工作,除非光标位于单词内。在这种情况下,它不应该改变任何东西的字体大小,除了如果用户写任何东西的文本的字体大小。问题是由于某些原因 TextSelection.ApplyPropertyValue(RichTextBox.FontSizeProperty, value)
当光标在一个词内时改变了整个词的字体大小。
这是我的事件处理程序:
private void fontSizeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
string value = (string)comboBox.SelectedValue;
if (comboBox.IsDropDownOpen)
{
TextSelection text = textBoxMain.Selection;
richTextBox.Focus();
text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value);
}
}
而且我不能在 if 语句中使用添加 !text.Text.IsEmpty
之类的东西,因为我仍然需要能够更改要写入的文本的字体大小。
我在 Whosebug 上发现了类似的问题,但 none 有一个实际可行的答案。
编辑:添加了 XAML
<Window x:Class="MathEdit.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"
xmlns:local="clr-namespace:MathEdit"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="Open" Executed="OpenCommandBinding_Executed"></CommandBinding>
<CommandBinding Command="Save" Executed="SaveCommandBinding_Executed"></CommandBinding>
<CommandBinding Command="SaveAs" Executed="SaveAsCommandBinding_Executed"></CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="O" Modifiers="Control" Command="Open"></KeyBinding>
<KeyBinding Key="S" Modifiers="Control" Command="Save"></KeyBinding>
<KeyBinding Key="S" Modifiers="Control+Alt" Command="SaveAs"></KeyBinding>
</Window.InputBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" InputGestureText="Ctrl+N" />
<MenuItem Header="_Open" InputGestureText="Ctrl+O" Command="Open"/>
<MenuItem Header="_Save" InputGestureText="Ctrl+S" Command="Save"/>
<MenuItem Header="_Save As" InputGestureText="Ctrl+Alt+S" Command="SaveAs"/>
<Separator />
<MenuItem Header="_Exit" InputGestureText="Alt+F4" />
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_Check if toby = on" IsCheckable="false" IsChecked="True" Click="MenuItem_Click" />
<MenuItem Header="_Settings" Click="MenuItem_Click_2" IsCheckable="True" />
<MenuItem Header="_Add formula" x:Name="menuItemAdd" Click="MenuItem_Add_Click" />
</MenuItem>
<ComboBox x:Name="fontSizeBox" Width="40" SelectedValuePath="Content" SelectionChanged="fontSizeBox_SelectionChanged" SelectedIndex="2">
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="12"/>
<ComboBoxItem Content="16"/>
<ComboBoxItem Content="20"/>
</ComboBox>
</Menu>
<Grid x:Name="gridParent">
<RichTextBox x:Name="richTextBox" AcceptsReturn="True" SelectionChanged="textBoxMain_SelectionChanged" />
</Grid>
</DockPanel>
将 属性 值简单应用到无文本 selection 是不可能的。看看当你将它应用到真正的 selection 时你会得到什么:假设你有一个文本 "hello brave new world"。 RTB 中的文档如下所示(简化)
<FlowDocument>
<Paragraph>
<Run>hello brave new world</Run>
</Paragraph>
</FlowDocument>
当您 select "brave" 并更改字体大小(或其他任何内容)时,文档将更改为
<FlowDocument>
<Paragraph>
<Run>hello </Run>
<Run FontSize="20">brave</Run>
<Run> new world</Run>
</Paragraph>
</FlowDocument>
必须将属性应用于某些文本;无法按照您的要求更改 "nothing"。如果你想实现在我看来最接近你的要求,你将不得不自己拆分文档 - 当 selection 为空时 - 并创建一个具有所需字体大小的空 运行。所以上面的例子看起来像这样(假设插入符号位于 "brave" 之前):
<FlowDocument>
<Paragraph>
<Run>hello </Run>
<Run FontSize="20"></Run>
<Run>brave new world</Run>
</Paragraph>
</FlowDocument>
因为这留下了无限的可能性,以无限次 "empty" 运行结束,我建议修改您的要求。
我正在尝试使用 RichTextBox 在 WPF 中创建一个文本编辑器。
我的问题是更改文本的字体大小。我的代码在每种情况下都按预期工作,除非光标位于单词内。在这种情况下,它不应该改变任何东西的字体大小,除了如果用户写任何东西的文本的字体大小。问题是由于某些原因 TextSelection.ApplyPropertyValue(RichTextBox.FontSizeProperty, value)
当光标在一个词内时改变了整个词的字体大小。
这是我的事件处理程序:
private void fontSizeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
string value = (string)comboBox.SelectedValue;
if (comboBox.IsDropDownOpen)
{
TextSelection text = textBoxMain.Selection;
richTextBox.Focus();
text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value);
}
}
而且我不能在 if 语句中使用添加 !text.Text.IsEmpty
之类的东西,因为我仍然需要能够更改要写入的文本的字体大小。
我在 Whosebug 上发现了类似的问题,但 none 有一个实际可行的答案。
编辑:添加了 XAML
<Window x:Class="MathEdit.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"
xmlns:local="clr-namespace:MathEdit"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="Open" Executed="OpenCommandBinding_Executed"></CommandBinding>
<CommandBinding Command="Save" Executed="SaveCommandBinding_Executed"></CommandBinding>
<CommandBinding Command="SaveAs" Executed="SaveAsCommandBinding_Executed"></CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="O" Modifiers="Control" Command="Open"></KeyBinding>
<KeyBinding Key="S" Modifiers="Control" Command="Save"></KeyBinding>
<KeyBinding Key="S" Modifiers="Control+Alt" Command="SaveAs"></KeyBinding>
</Window.InputBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" InputGestureText="Ctrl+N" />
<MenuItem Header="_Open" InputGestureText="Ctrl+O" Command="Open"/>
<MenuItem Header="_Save" InputGestureText="Ctrl+S" Command="Save"/>
<MenuItem Header="_Save As" InputGestureText="Ctrl+Alt+S" Command="SaveAs"/>
<Separator />
<MenuItem Header="_Exit" InputGestureText="Alt+F4" />
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_Check if toby = on" IsCheckable="false" IsChecked="True" Click="MenuItem_Click" />
<MenuItem Header="_Settings" Click="MenuItem_Click_2" IsCheckable="True" />
<MenuItem Header="_Add formula" x:Name="menuItemAdd" Click="MenuItem_Add_Click" />
</MenuItem>
<ComboBox x:Name="fontSizeBox" Width="40" SelectedValuePath="Content" SelectionChanged="fontSizeBox_SelectionChanged" SelectedIndex="2">
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="12"/>
<ComboBoxItem Content="16"/>
<ComboBoxItem Content="20"/>
</ComboBox>
</Menu>
<Grid x:Name="gridParent">
<RichTextBox x:Name="richTextBox" AcceptsReturn="True" SelectionChanged="textBoxMain_SelectionChanged" />
</Grid>
</DockPanel>
将 属性 值简单应用到无文本 selection 是不可能的。看看当你将它应用到真正的 selection 时你会得到什么:假设你有一个文本 "hello brave new world"。 RTB 中的文档如下所示(简化)
<FlowDocument>
<Paragraph>
<Run>hello brave new world</Run>
</Paragraph>
</FlowDocument>
当您 select "brave" 并更改字体大小(或其他任何内容)时,文档将更改为
<FlowDocument>
<Paragraph>
<Run>hello </Run>
<Run FontSize="20">brave</Run>
<Run> new world</Run>
</Paragraph>
</FlowDocument>
必须将属性应用于某些文本;无法按照您的要求更改 "nothing"。如果你想实现在我看来最接近你的要求,你将不得不自己拆分文档 - 当 selection 为空时 - 并创建一个具有所需字体大小的空 运行。所以上面的例子看起来像这样(假设插入符号位于 "brave" 之前):
<FlowDocument>
<Paragraph>
<Run>hello </Run>
<Run FontSize="20"></Run>
<Run>brave new world</Run>
</Paragraph>
</FlowDocument>
因为这留下了无限的可能性,以无限次 "empty" 运行结束,我建议修改您的要求。