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
Post a Comment