.net - Start a process from a bound wpf hyperlink -
what's simple way have wpf hyperlink run process.start
on (data-bound) text content?
one way (that improved refactoring usercontrol or static class or valueconverter or ...) have hyperlink bound command on viewmodel , pass in content of hyperlink (i couldn't find way relative binding) command parameter.
wpf snippet
<textblock> <textblock.datacontext> <viewmodels:viewmodel /> </textblock.datacontext> <hyperlink command="{binding launchhyperlinkcommand}" commandparameter="{binding elementname=_content, path=text}"> <run text="{binding filepath, mode=oneway}" name="_content"/> </hyperlink> </textblock>
c# datacontext class snippet (note: using prism)
public class viewmodel : notificationobject { public string filepath { get; private set; } public delegatecommand<string> launchhyperlinkcommand { get; set; } public viewmodel() { launchhyperlinkcommand = new delegatecommand<string>(launchhyperlink); } private static void launchhyperlink(string link) { try { process.start(link); } catch (exception e) { messagebox.show(e.message); } } }
Comments
Post a Comment