i want make textbox text hyperlink. suppose if type www.google.com tetxbox text,when click on text ,it show open link in browser..
i not idea implement this...can u suggest me idea...i tried 2 ways.
way1:
<textbox grid.row="4" name="txtwebpage" verticalalignment="top" textdecorations="underline" textchanged="txtwebpage_textchanged" foreground="blue"> </textbox>
way2:
<textblock name="tbwebpage" grid.row="4" background="white" verticalalignment="top" height="20" > <hyperlink></hyperlink> </textblock>
way3:
<richtextbox grid.row="4" name="rtxtweb" borderbrush="gray" borderthickness="1" verticalalignment="top" height="20" isdocumentenabled="true" foreground="blue" lostfocus="rtxtweb_lostfocus"> <flowdocument> <paragraph> <hyperlink navigateuri=""/> </paragraph> </flowdocument> </richtextbox>
i couldnt how can bind richtextbox text hyperlink uri!!! there no click event richtextbox...any suggestion please...
first, i'm not sure why you'd want this... if text becomes clickable hyperlink instant it's valid uri, how continue edit it?
the hyperlink control doesn't special , can't hosted in textbox. instead, use regular textbox, check text valid uri every time it's updated, , apply style make text clickable link.
<textbox textchanged="textbox_textchanged" mousedoubleclick="textbox_mousedoubleclick"> <textbox.style> <style targettype="textbox"> <style.triggers> <datatrigger binding="{binding hasvaliduri}" value="true"> <setter property="textdecorations" value="underline"/> <setter property="foreground" value="#ff2a6dcd"/> </datatrigger> </style.triggers> </style> </textbox.style> </textbox>
every time text changed, textbox_textchanged
called. checks whether text valid uri using uri.trycreate()
. if so, property hasvaliduri
set true
. datatrigger
in textbox's
style picks , makes text underlined , blue.
making hyperlink clickable cause not able position cursor, watch double-click instead. when 1 received, convert text uri again , kick off process
uri.
public partial class mainwindow : window, inotifypropertychanged { private bool _hasvaliduri; public bool hasvaliduri { { return _hasvaliduri; } set { _hasvaliduri = value; onpropertychanged( "hasvaliduri" ); } } public event propertychangedeventhandler propertychanged; public void onpropertychanged( string name ) { var handler = propertychanged; if( handler != null ) handler( this, new propertychangedeventargs( name ) ); } public mainwindow() { initializecomponent(); this.datacontext = this; } private void textbox_textchanged( object sender, textchangedeventargs e ) { uri uri; hasvaliduri = uri.trycreate( (sender textbox).text, urikind.absolute, out uri ); } private void textbox_mousedoubleclick( object sender, mousebuttoneventargs e ) { uri uri; if( uri.trycreate( (sender textbox).text, urikind.absolute, out uri ) ) { process.start( new processstartinfo( uri.absoluteuri ) ); } } }
Comments
Post a Comment