Friday 15 June 2012

wpf - FlowDocument: Insert Hyperlink and keep formatting -



wpf - FlowDocument: Insert Hyperlink and keep formatting -

how insert hyperlink flowdocument @ specific position? new hyperlink should have same formatting surrounding text - except color (should blue) , underline (should underlined). after inserting hyperlink, cursor should right after new hyperlink.

things i've tried:

textpointer caretposition = richtextbox.caretposition; textpointer insertposition = caretposition.isatinsertionposition ? caretposition : caretposition.getinsertionposition(logicaldirection.forward); string linktitle = "stack overflow"; run run = new run(linktitle); hyperlink link = new hyperlink(run, insertposition); link.isenabled = true; link.navigateuri = new uri(@"http://www.stackoverflow.com"); richtextbox.caretposition = run.contentend;

this works, new hyperlink not have formatting of it's surrounding text.

this.caretposition = this.caretposition.getpositionatoffset(0, logicaldirection.forward); insertposition.inserttextinrun(linktitle); hyperlink link = new hyperlink(insertposition, insertposition.getpositionatoffset(linktitle.length)); link.isenabled = true; link.navigateuri = new uri(@"http://www.stackoverflow.com"); textpointer positionafterlink = link.contentend.getpositionatoffset(1); if (!positionafterlink.isatinsertionposition) positionafterlink = caretposition.getinsertionposition(logicaldirection.forward); richtextbox.caretposition = positionafterlink;

this way, formatting kept. sometimes, caretposition not set behind end of new link, stays in front end of new link. also, doesn't robust me.

has done that? right way this?

i edit first code.the richtextbox.selection textrange having method called getpropertyvalue allowing format properties such fontstyle, fontweight, fontsize (and think that's enough). can set achieved format info newly created hyperlink:

textpointer caretposition = richtextbox.caretposition; textpointer insertposition = caretposition.isatinsertionposition ? caretposition : caretposition.getinsertionposition(logicaldirection.forward); string linktitle = "stack overflow"; //try getting format info var fstyle = (fontstyle) richtextbox.selection .getpropertyvalue(control.fontstyleproperty); var fweight =(fontweight)richtextbox.selection .getpropertyvalue(control.fontweightproperty); var fsize = (double)richtextbox.selection .getpropertyvalue(control.fontsizeproperty); //create new link run run = new run(linktitle); hyperlink link = new hyperlink(run, insertposition); link.fontstyle = fstyle; link.fontweight = fweight; link.fontsize = fsize; //remaining code ...

to create link have default style (blue color , underlined), have set richtextbox's property isdocumentenabled true. after inserting, may want phone call focus() on richtextbox.

wpf hyperlink insert formatting flowdocument

No comments:

Post a Comment