处理DataGridHyperlinkColumn单击事件 [英] Handle DataGridHyperlinkColumn Click Event
问题描述
如何通过代码(.xaml.cs文件)以编程方式处理DataGridHyperlinkColumn的点击事件。
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);
这个堆栈溢出答案也有很好的信息WPF工具包的Data GridHyperlinkColumn,值得一试。
This stack overflow answer also has good info on the WPF toolkit's Data GridHyperlinkColumn, well worth checking out.
这篇关于处理DataGridHyperlinkColumn单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!