wpf - CustomRichTextBox StrikeThrough TextDecoration -


i want add textdecorations.strikethrough decoration button custom richtextbox using code bellow adding , removing textdecoration thing getting invalidcastexception: unable cast object of type 'ms.internal.namedobject' type 'system.windows.textdecorationcollection'. when selecting range greater 1 strikedthrough , clicking "strikethrough" button.

my code

private void strikeoutbutton_click(object sender, routedeventargs e)     {         textrange range = new textrange(this.myrichtextbox.selection.start,                                       this.myrichtextbox.selection.end);          textdecorationcollection tdc =             (textdecorationcollection)this.myrichtextbox.                  selection.getpropertyvalue(inline.textdecorationsproperty);         /*         if (tdc == null || !tdc.equals(textdecorations.strikethrough))         {             tdc = textdecorations.strikethrough;         }         else         {             tdc = new textdecorationcollection();         }          * */         if (tdc == null || !tdc.contains(textdecorations.strikethrough[0]))         {             tdc = textdecorations.strikethrough;         }         else         {             tdc = new textdecorationcollection();         }          range.applypropertyvalue(inline.textdecorationsproperty, tdc);     } 

the comment out code not working.

i going post exceptiondetails think it's clear.

can provide me workaround?

the issue dependencyproperty.unsetvalue if not complete text either decorated strikethrough or not.

so may check dependencyproperty.unsetvalue , apply strikethrough in case.

i made short test , solutions works me:

private void strikeoutbutton_click(object sender, routedeventargs e)     {         textrange textrange = new textrange(textbox.selection.start, textbox.selection.end);         var currenttextdecoration = textrange.getpropertyvalue(inline.textdecorationsproperty);          textdecorationcollection newtextdecoration;          if (currenttextdecoration != dependencyproperty.unsetvalue)             newtextdecoration = ((textdecorationcollection)currenttextdecoration == textdecorations.strikethrough) ? new textdecorationcollection() : textdecorations.strikethrough;         else             newtextdecoration = textdecorations.strikethrough;          textrange.applypropertyvalue(inline.textdecorationsproperty, newtextdecoration);     } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -