How to handle click event of DataGridHyperlinkColumn programatically through code(in .xaml.cs file).
If you just want to navigate the browser to the link, it's a simple as writing a handler like this:
void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
var destination = ((Hyperlink) e.OriginalSource).NavigateUri;
Process.Start(destination.ToString());
}
If you instead want to take some custom action upon navigation, using information in the associated row, then you will need to access the data context of the hyperlink:
void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
var rowData = ((Hyperlink) e.OriginalSource).DataContext as User;
navigationService.NavigateToUserRecordForId(rowData.Id);
}
If you want to programatically create a hyperlink column, and bind to it's click event, you can do this:
var style = new Style(typeof(TextBlock));
style.Setters.Add(new EventSetter(Hyperlink.ClickEvent, (RoutedEventHandler)EventSetter_OnHandler));
var column = new DataGridHyperlinkColumn { Header = "User", Binding = new Binding("ViewUserLink"), ElementStyle = style };
dataGrid1.Columns.Add(column);
This stack overflow answer also has good info on the WPF toolkit's Data GridHyperlinkColumn, well worth checking out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With