我正在使用标准的WPFRichTextBox
控件。
我可以成功设置前景色,但是设置背景色会出现以下错误:
System.ArgumentException:“背景”属性对于文本格式无效。
这是我正在测试的代码:
// SUCCESS
this.rtfDocument.Selection.ApplyPropertyValue(
System.Windows.Controls.RichTextBox.ForegroundProperty,
System.Windows.Media.Brushes.Red);
// ERROR
this.rtfDocument.Selection.ApplyPropertyValue(
System.Windows.Controls.RichTextBox.BackgroundProperty,
System.Windows.Media.Brushes.Blue);
我正在使用System.Windows.Media
其他Stackoverflow问题提到的名称空间笔刷。
编辑:
有趣的是,即使获取背景色也会引发此错误:
// SUCCESS
var f = this.rtfDocument.Selection.GetPropertyValue(
System.Windows.Controls.RichTextBox.ForegroundProperty);
// ERROR
var b = this.rtfDocument.Selection.GetPropertyValue(
System.Windows.Controls.RichTextBox.BackgroundProperty);
也许错误与实际属性本身有关?
该TextRange.ApplyPropertyValue
方法将属性值应用于文档元素,而不应用于RichTextBox本身。
因此,请勿设置RichTextBox属性,而应设置TextElement属性:
rtfDocument.Selection.ApplyPropertyValue(
System.Windows.Documents.TextElement.ForegroundProperty,
System.Windows.Media.Brushes.Red);
rtfDocument.Selection.ApplyPropertyValue(
System.Windows.Documents.TextElement.BackgroundProperty,
System.Windows.Media.Brushes.Blue);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句