Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically removing Strikethrough TextDecoration from code behind in WPF

Tags:

c#

.net

wpf

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

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.

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

like image 305
Bernd L. Avatar asked Sep 14 '25 10:09

Bernd L.


1 Answers

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;
}
like image 157
sa_ddam213 Avatar answered Sep 16 '25 01:09

sa_ddam213