Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle DataGridHyperlinkColumn Click Event

Tags:

wpf

datagrid

How to handle click event of DataGridHyperlinkColumn programatically through code(in .xaml.cs file).

like image 268
Abhijeet Kumar Avatar asked Nov 18 '09 11:11

Abhijeet Kumar


1 Answers

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.

like image 163
Bittercoder Avatar answered Nov 14 '22 05:11

Bittercoder