Using WPF DataGridHyperLinkColumn Items to open Windows Explorer and open files
This works universally:
<DataGridHyperlinkColumn Binding="{Binding Link}"> <DataGridHyperlinkColumn.ElementStyle> <Style> <EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/> </Style> </DataGridHyperlinkColumn.ElementStyle></DataGridHyperlinkColumn>
private void DG_Hyperlink_Click(object sender, RoutedEventArgs e){ Hyperlink link = (Hyperlink)e.OriginalSource; Process.Start(link.NavigateUri.AbsoluteUri);}
If the URI points a website it will be opened with the default web-browser, if it is a folder it will be opened in explorer, if it is a file it will be opened with the default application associated with it.
To use this for autogenerated columns your property needs to be of type Uri
so a DataGridHyperlinkColumn
is generated. You then can hook up the event by placing the style in the DataGrid.Resources
:
<DataGrid.Resources> <Style TargetType="Hyperlink"> <EventSetter Event="Click" Handler="DG_Hyperlink_Click"/> </Style></DataGrid.Resources>