【问题标题】:Programatically removing Strikethrough TextDecoration from code behind in WPF以编程方式从 WPF 中的代码中删除删除线文本装饰
【发布时间】:2017-07-21 20:08:25
【问题描述】:

我在 WPF 桌面应用程序中实现以下行为时遇到问题:

我从后面的代码动态创建 TextBlock 并将它们插入 StackPanel。到目前为止,这有效。当用户将鼠标移到 TextBlock 上时,应将删除线应用于 textblock,表示可以通过单击删除该项目。同样,这仍然有效。当鼠标离开 textblock 时,删除线将被移除,这里会抛出异常,说 IsFrozen 必须设置为 false 才能更改 TextDecorationCollection 对象。我无法弄清楚如何解决这个问题。

这是我的代码:

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]);
}

任何帮助将不胜感激。

谢谢, 伯恩德

【问题讨论】:

  • 你可以设置成null, tbl.TextDecorations = null;
  • 太好了,成功了!非常感谢!
  • 如果上述评论符合您的要求,您可能需要回答并关闭此问题
  • 我已经尝试过了,但我必须等待 8 小时才能回答我自己的问题 :(

标签: c# .net wpf


【解决方案1】:

我发现以下最适合我:

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);
}

显然这是为了去除下划线装饰,但我想删除线也不例外。

【讨论】:

  • 这应该是答案。
  • 谢谢。我一直在寻找这个。
【解决方案2】:

您可以将TextDecorations 设置为null,这将清除TextBlock 中的Strikethrough 装饰

private void HandleAddedSecondaryDxMouseLeave(Object sender, MouseEventArgs e)
{
    TextBlock tbl = (TextBlock)sender;
    tbl.TextDecorations = null;
}

【讨论】:

    【解决方案3】:

    我使用下面的代码来删除文本范围的下划线。同样适用于 TextBlock。

    TextDecorationCollection textDecorations;
    (textRange.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection).TryRemove(TextDecorations.Underline, out textDecorations);
    textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, textDecorations);
    

    【讨论】:

      猜你喜欢
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2013-02-21
      相关资源
      最近更新 更多