Handle DataGridHyperlinkColumn Click Event

后端 未结 2 818
南旧
南旧 2020-12-31 08:23

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

相关标签:
2条回答
  • 2020-12-31 09:05

    use this:

    <dg:DataGridHyperlinkColumn.ElementStyle>
    <Style TargetType="TextBlock">
    <EventSetter Event="Hyperlink.Click" Handler="OnHyperlinkClick" />
    </Style>
    </dg:DataGridHyperlinkColumn.ElementStyle>
    </dg:DataGridHyperlinkColumn>
    
    0 讨论(0)
  • 2020-12-31 09:19

    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.

    0 讨论(0)
提交回复
热议问题