C# Windows窗体中承载的WPF RichTextbox:跟踪按钮状态
我已经创建了一个基于Windows窗体的WPF RichTextbox控件。 这是基于布赖恩·拉古纳斯(Brian Lagunas)在 它工作得很好,但最近报告了一个问题,我无法找到原因。 Brian确实警告读者要处理按钮状态,但我认为我做得很好 例如: 提供了以下文本: 第1款 我关掉了粗体和下划线,它似乎工作正常。 威廉·布莱克·里士满1842年出生于伦敦。他的父亲,乔治·里士满,R.A.(1809-1896),他自己是托马斯·里士满的儿子,画了他那个时代最杰出人物的肖像,并在社会上发挥了作用。他是以他父亲的密友、艺术家威廉·布莱克的名字命名的 如果我突出显示“段落1”,按backspace,然后使用键盘快捷键Ctrl+B和Ctrl+U启用“粗体”和“下划线”,然后开始键入,则单词将显示为未格式化。工具栏上的粗体和下划线按钮也会被取消标记\取消选中 下面的代码块显示了快捷方式控件的KeyUp事件的处理C# Windows窗体中承载的WPF RichTextbox:跟踪按钮状态,c#,wpf,winforms,formatting,toolbar,C#,Wpf,Winforms,Formatting,Toolbar,我已经创建了一个基于Windows窗体的WPF RichTextbox控件。 这是基于布赖恩·拉古纳斯(Brian Lagunas)在 它工作得很好,但最近报告了一个问题,我无法找到原因。 Brian确实警告读者要处理按钮状态,但我认为我做得很好 例如: 提供了以下文本: 第1款 我关掉了粗体和下划线,它似乎工作正常。 威廉·布莱克·里士满1842年出生于伦敦。他的父亲,乔治·里士满,R.A.(1809-1896),他自己是托马斯·里士满的儿子,画了他那个时代最杰出人物的肖像,并在社会上发挥了作
private void RTFBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
UpdateButtonsStateBasedOnKeys(e);
}
private void UpdateButtonsStateBasedOnKeys(System.Windows.Input.KeyEventArgs e)
{
if (e.KeyboardDevice.Modifiers == System.Windows.Input.ModifierKeys.Control &&
e.Key == System.Windows.Input.Key.B)
{
UpdateButtonsState(_ToolStrip.BoldButton);
}
else if (e.KeyboardDevice.Modifiers == System.Windows.Input.ModifierKeys.Control &&
e.Key == System.Windows.Input.Key.U)
{
UpdateButtonsState(_ToolStrip.UnderlineButton);
}
// Removed other if blocks for brevity
UpdateTextRangeFormattingState();
}
public void UpdateButtonsState(System.Windows.Forms.ToolStripButton button)
{
//Other conditions removed
UpdateButtonCheckedState(button);
}
private void UpdateButtonCheckedState(System.Windows.Forms.ToolStripButton button)
{
button.Checked = !button.Checked;
}
private void UpdateTextRangeFormattingState()
{
UpdateTextRangeFormatting(_ToolStrip.BoldButton, TextElement.FontWeightProperty, FontWeights.Bold, FontWeights.Regular);
UpdateTextRangeFormatting(_ToolStrip.UnderlineButton, Inline.TextDecorationsProperty, TextDecorations.Underline, null);
// Removed other statements for brevity
}
private void UpdateTextRangeFormatting(System.Windows.Forms.ToolStripButton button, DependencyProperty formattingProperty, object expectedValue, object oppositeValue)
{
TextPointer position = _RTFBox.TextSelection.End;
if (position != null)
{
TextRange range = new TextRange(position, _RTFBox.TextSelection.End);
if (button.Checked)
{
range.ApplyPropertyValue(formattingProperty, expectedValue);
}
else
{
range.ApplyPropertyValue(formattingProperty, oppositeValue);
}
}
}
下面的块对应于selectionchanged事件
private void Box_SelectionChanged(object sender, RoutedEventArgs e)
{
UpdateButtonsStateWithSelection();
}
private void UpdateButtonsStateWithSelection()
{
TextPointer position = _RTFBox.TextSelection.End.GetPositionAtOffset(-1);
if (position != null)
{
TextRange range = new TextRange(position, _RTFBox.TextSelection.End);
if (range.IsEmpty)
{
position = _RTFBox.TextSelection.End.GetPositionAtOffset(1);
if (position != null)
{
range = new TextRange(position, _RTFBox.TextSelection.End);
}
}
if (!range.IsEmpty)
{
UpdateAllButtonsCheckedState();
}
}
}
private void UpdateAllButtonsCheckedState()
{
UpdateButtonCheckedState(_ToolStrip.BoldButton, TextElement.FontWeightProperty, FontWeights.Bold, FontWeights.Regular);
UpdateButtonCheckedState(_ToolStrip.UnderlineButton, Inline.TextDecorationsProperty, TextDecorations.Underline, null);
// Other statements removed for brevity
}
private void UpdateButtonCheckedState(System.Windows.Forms.ToolStripButton button, DependencyProperty formattingProperty,
object expectedValue, object oppositeValue)
{
object value = _SpellBox.TextSelection.GetPropertyValue(formattingProperty);
if (value != null)
{
if (value.Equals(expectedValue))
{
button.Checked = true;
}
else
{
button.Checked = false;
}
}
}
对于我来说,它为什么会丢失格式的问题并不明显。
我怀疑它就在这两者之中,但也许我弄错了,或者我误解了什么。任何指点都将不胜感激。谢谢
编辑:2014-06-18 新代码段:
private void UpdateButtonsStateWithSelection()
{
TextRange range = new TextRange(_SpellBox.CaretPosition, _SpellBox.CaretPosition);
if (range != null)
{
UpdateAllButtonsCheckedState();
}
}
对我来说,状态更新所需的文本范围仅仅是CaretPosition,这与直觉相反。我在2008年10月看到一篇文章,我认为到目前为止,这个实现似乎解决了我的问题。我已经用Donnelle提供的代码片段替换了UpdateButtonStateWithSelection方法的内容。