Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

wpf - Handle DataGridHyperlinkColumn Click Event

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...