以编程方式从 WPF 中的代码中删除删除线 TextDecoration
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16480344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programatically removing Strikethrough TextDecoration from code behind in WPF
提问by Bernd L.
I have problems achieving the following behaviour in my WPF desktop application:
我在 WPF 桌面应用程序中实现以下行为时遇到问题:
I dynamically create TextBlocks from code behind and insert them into a StackPanel. This works so far. When the user moves the mouse over the TextBlock, a Strikthrough shall be applied to the textblock, indicating that the item can be deleted by clicking on it. Again, this still works. When the mouse leaves the textblock, the strikethrough shall be removed, and here's where the exception is thrown saying that IsFrozen must be set to false in order to change the TextDecorationCollection object. I wasn't able to figure out how to get around this.
我从后面的代码动态创建 TextBlocks 并将它们插入到 StackPanel 中。到目前为止,这有效。当用户在将TextBlock移动鼠标,Strikthrough应被施加到文本块,指示该物品可以通过点击它来删除。同样,这仍然有效。当鼠标离开textblock 时,删除线将被删除,这里是抛出异常的地方,说明必须将 IsFrozen 设置为 false 才能更改 TextDecorationCollection 对象。我无法弄清楚如何解决这个问题。
Here's my code:
这是我的代码:
private void HandleAddedSecondaryDxMouseEnter(Object sender, MouseEventArgs e) {
TextBlock tbl = (TextBlock)sender;
tbl.TextDecorations = TextDecorations.Strikethrough;
}
private void HandleAddedSecondaryDxMouseLeave(Object sender, MouseEventArgs e) {
TextBlock tbl = (TextBlock)sender;
tbl.TextDecorations.Remove(tbl.TextDecorations[0]);
}
Any help would be greatly appreciated.
任何帮助将不胜感激。
Thanks, Bernd
谢谢,伯恩德
回答by OpusAckPht
I have found the following to work best for me:
我发现以下最适合我:
TextDecorationCollection decs = (TextDecorationCollection)theRTB.Selection.GetPropertyValue( Inline.TextDecorationsProperty );
if (decs.Contains(TextDecorations.Underline[0]))
{
TextDecorationCollection noUnder = new TextDecorationCollection(decs);
noUnder.Remove(TextDecorations.Underline[0]); //this is a bool, and could replace Contains above
theRTB.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, noUnder);
}
Obviously this is to remove the underline decoration, but I imagine that strikethrough would be no different.
显然这是为了去除下划线装饰,但我想删除线也不例外。
回答by sa_ddam213
You can set the TextDecorationsto null, this will clear the Strikethroughdecoration from the TextBlock
您可以将 设置TextDecorations为null,这将从中清除Strikethrough装饰TextBlock
private void HandleAddedSecondaryDxMouseLeave(Object sender, MouseEventArgs e)
{
TextBlock tbl = (TextBlock)sender;
tbl.TextDecorations = null;
}
回答by Byju
I have used below code to remove underline for a text range. Same should work for TextBlock as well.
我使用下面的代码来删除文本范围的下划线。同样也适用于 TextBlock。
TextDecorationCollection textDecorations;
(textRange.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection).TryRemove(TextDecorations.Underline, out textDecorations);
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, textDecorations);

