WPF RichTextbox 从 TextRange 中删除前景信息
抱歉我的英语不好... RichTextBox
内容的默认设置是从 RichTextBox
本身继承前景色。这很好,但如果我为文本的某些部分设置特定的前景
颜色,那么显然该部分不再继承前景
。如何让我的“彩色”文本再次继承 Foreground
?我正在尝试执行类似于 Office Word 中的“自动”颜色的操作,但在为 TextRange
设置特定颜色后,我不知道如何取消设置它:/
TextRange. ClearAllProperties()
可以满足我的需要,但也会删除其他属性,例如 FontSize
和 FontFamily
...
TextRange.ApplyPropertyValue(ForegroundProperty, DependencyProperty.UnsetValue )
也没有达到目的...
sorry for my bad english... The default for a RichTextBox
content is to inherit the Foreground color from the RichTextBox
itself. That's nice, but if I set a specific Foreground
color to some part of my text, that part does not inherit the Foreground
anymore, obviously. How can I make my "colored" text inherit the Foreground
again? I'm trying to do something like the "Automatic" color from Office Word but after I have set a specific color to a TextRange
, I do not know how to unset it :/
TextRange.ClearAllProperties()
does what I need, but also erases other properties like FontSize
and FontFamily
...
TextRange.ApplyPropertyValue(ForegroundProperty, DependencyProperty.UnsetValue)
also does not do the trick...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以通过将属性设置为 null 来取消设置(这对我清除背景很有用,例如删除突出显示)
TextRange.ApplyPropertyValue(TextElement.BackgroundProperty, null);
You can also unset it by setting the property to null (this worked for me clearing out the background, for example removing highlighting)
TextRange.ApplyPropertyValue(TextElement.BackgroundProperty, null);
这似乎几乎不可能实现,因为没有“RemovePropertyValue”方法。我也尝试过使用跨度,并得到了与您相同的异常,所以我创建了一种方法来收集 TextRange 内的所有段落,并为每个段落分别创建一个跨度.. 不太理想,我知道.. 无论如何,它适用于一个小的例如,但对于更复杂的东西来说可能很难使用。
This seemed almost impossible to achieve since there is no "RemovePropertyValue" method. I also tried with span and got the same exception as you did so I made a method that collects all the Paragraphs within the TextRange and made a span for each separetly.. less than ideal, I know.. Anyway, it works for a small example but might be pretty hard to work with for something more complex.
如果您查看 .NET 参考源中的方法
TextRange.ApplyPropertyValue
的代码,您会发现最终它对DependencyObject.SetValue
的集合调用DependencyObject.SetValue
。 code>Inline 和Block
以及DependencyObject.SetValue
通过有效清除本地值来特殊对待DependencyProperty.UnsetValue
值为了财产。问题是他们在实现 TextRange.ApplyPropertyValue 时没有想到这一点:它根据属性类型检查传递的属性值,并且在引用类型的情况下,它确保传递的值是null 或从同一个类继承,从而阻止我们传递 DependencyProperty.UnsetValue 。
我发现的一个解决方案用于实现一种清除 引用类型 的依赖属性的
TextRange
本地值的方法,如下所示:If you look at the code of method
TextRange.ApplyPropertyValue
in the .NET Reference Source, you'll see that in the end it callsDependencyObject.SetValue
on a collection ofInline
s andBlock
s, andDependencyObject.SetValue
treats the valueDependencyProperty.UnsetValue
specially by effectively clearing the local value for the property.The problem is that they didn't think of that when implementing
TextRange.ApplyPropertyValue
: it checks the passed property value against the property type, and in case of a reference type, it makes sure the passed value is either null or inherits from the same class, thus preventing us from passingDependencyProperty.UnsetValue
.One solution I found to implement a way of clearing local values of a
TextRange
for dependency properties of a reference type is the following: