c# - In WPF and MVVM, how to add a "Copy" context menu to a <Hyperlink>? -


Most web browsers can right click on a hyperlink and "copy link address ..." of context menu Can be used.

In WPF, I'm wondering if & lt; Hyperlink & gt; Is there a method to add the same functionality to the XAML tag?

Using MVVM light M.

It should really be more difficult.

The reason that, that is, that the attempt to use any of the most logical binding is instead of the expected value of the returns.

The solution is to wrap the entire hyperlink into a UserControl, then use the {binding placements target.Compatible in this case to use the properties we want, The required asset URL is what we need the parameter when we want to copy the hyperlink to the clipboard through the context menu. Of course, we can specify the URL twice, but it violates the principle of DRY (not repeat itself)

I am using.

XAML

The second command parameter is aimed at navigating in the oriented hyperlink tag > And pass as the parameter for the context menu, so it can be copied to the clipboard.

  & lt; UserControl & gt; & Lt; Hyperlink NavigateUri = "http://www.google.com/" command = "{Binding OnClickHyperlink}" CommandParameter = & gt; "{NavigateUri, RelativeSource = {RelativeSource} Binding}"; Www.google.com & lt; Hyperlink Contact Menu & gt; & Lt; ContextMenu & gt; & Lt; MenuItem header = "Copy Link Address" command = "{Binding OnCopyHyperlink}" CommandParameter = "{PlacementTarget.Content.NavigateUri Binding, RelativeSource = {RelativeSource Mode = FindAncestor, AncestorType = ContextMenu}}" & gt; & Lt; / MenuItem & gt; & Lt; / ContextMenu & gt; & Lt; /Hyperlink.ContextMenu> & Lt; / Hyperlink & gt; & Lt; / UserControl & gt;  

C # for hyperlink click

  Private ICommand _onClickHyperlink; Public ICDD OnclickHyperLink {Received {Return _onClickHyperlink ?? (Click here using _onClickHyperlink = New Relay Command & lt; Uri & gt; (Hyperlink = & gt; {// Handle Hyperlink process.)}}}); }}  

for hyperlink copy C #

  Private ICommand _onCopyHyperlink; Public ICDOM OnCopyHyperLink {Get {Return _CopyHyperLink ?? (_onCopyHyperlink = New RelayCommand & LT; Uri & gt; (HyperLink => {Clipboard.SetText (hyperlink.OriginalString);}); }}  

Comments