本文介绍了以编程方式从WPF后面的代码中删除Strikethrough TextDecoration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF桌面应用程序中无法实现以下行为:

I have problems achieving the following behaviour in my WPF desktop application:

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

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.

这是我的代码:

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

任何帮助将不胜感激.

谢谢,伯恩德

推荐答案

您可以将 TextDecorations 设置为 null ,这将清除删除线 TextBlock

You can set the TextDecorations to null, this will clear the Strikethrough decoration from the TextBlock

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

这篇关于以编程方式从WPF后面的代码中删除Strikethrough TextDecoration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:03