How to open DataGridHyperlinkColumn Links in the default browser by event? #460
-
I have the following layout but trying to open the link from the application by clicking on the column item causes the link to open in the application itself, instead of the default browser. Snippet of my setup <Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<DataGrid
Margin="0,20,0,0"
IsReadOnly="True"
CanUserAddRows="False"
CanUserSortColumns="True"
CanUserDeleteRows="False"
CanUserResizeRows="False"
CanUserResizeColumns="True"
CanUserReorderColumns="False"
AutoGenerateColumns="False"
ItemsSource="{Binding ViewModel.DataGridItemCollection}">
<DataGrid.Columns>
<DataGridHyperlinkColumn
Header="URL"
Binding="{Binding URLID}"
ContentBinding="{Binding URLID}"
MinWidth="25"
Width="*" />
</DataGrid.Columns>
</DataGrid>
</Grid> For testing purposes, I have bound the data to From research done, one should be able to assign an hyperlink click event but this does not work with the current setup. See https://stackoverflow.com/questions/2474801/wpf-datagrid-datagridhyperlinkcolumn-bound-to-uri. Is there something I am missing or is this not supported? If it is not supported, how may I achieve such functionality, thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Deduced a workaround for this using the <Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<DataGrid
Margin="0,20,0,0"
IsReadOnly="True"
CanUserAddRows="False"
CanUserSortColumns="True"
CanUserDeleteRows="False"
CanUserResizeRows="False"
CanUserResizeColumns="True"
CanUserReorderColumns="False"
AutoGenerateColumns="False"
ItemsSource="{Binding ViewModel.DataGridItemCollection}">
<DataGrid.Columns>
<DataGridHyperlinkColumn Header="URL" Binding="{Binding APIURLID.Uri}" ContentBinding="{Binding APIURLID.Content}" Width="55" >
<DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<EventSetter Event="Hyperlink.RequestNavigate" Handler="OnApiUriClicked"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
</DataGrid.Columns>
</DataGrid>
</Grid> To ensure that the application does not try to open the link also, one has to mark the event as handled after opening the URI in the default web browser. private void OnApiUriClicked(object sender, RoutedEventArgs e)
{
if (((System.Windows.Documents.Hyperlink)e.OriginalSource).DataContext is not ProductDescriptor rowData)
{
return;
}
try
{
Debug.WriteLine("Opening url in default web browser. {0}", rowData);
/// Other code
}
catch (Exception exception)
{
Debug.WriteLine("Encountered an exception opening Uri. {0}", exception.Message);
}
e.Handled = true;
} |
Beta Was this translation helpful? Give feedback.
Deduced a workaround for this using the
DataHyperlinkColumn
. The workaround ensures that the hyperlinkRequestNavigate
event is handled to prevent the application navigating to it using the in-built web view. My setup is stated below.