本文介绍了通过 C# WPF 表单中的代码在网络浏览器(IE 或 chrome)中打开超链接 URI(www.google.com)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个包含超链接的项目.我使用 DataGridHyperlinkColumn 在单个列中填充 URI.每当我单击 URI 时,它都会在浏览器和 wpf 窗口中打开.我可以使用 hyperlink.RequestNavigate() 阻止 URI 在 Web 浏览器中打开.

Im working on a project which has hyperlinks in it. I used DataGridHyperlinkColumn to populate the URI's in a single column. Whenever I click on a URI, it gets opened in both browser and the wpf window. I could prevent the URI opening in web browser by using hyperlink.RequestNavigate().

但我只需要在 Web 浏览器中而不是在 WPF 窗口中打开 URI.我在下面附上了我的 XAML 和 C# 代码.

but I need the URI to be opened only in web browser and not in the WPF window. I have attached my XAML and C# code below.

<DataGrid Name="dgUsers"  AutoGenerateColumns="False" CanUserAddRows="False" BorderThickness="0" CanUserResizeRows="False" GridLinesVisibility="None" HeadersVisibility="None" Focusable="False" Visibility="{Binding DgVisibility}">
            <DataGrid.Resources>
                <Style TargetType="Hyperlink">
                    <EventSetter Event="Click" Handler="DG_Hyperlink_Click"/>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>

                <DataGridHyperlinkColumn Binding="{Binding WebSite}"  Width="*" CanUserResize="False" CanUserSort="False" IsReadOnly="True"  >
                <DataGridHyperlinkColumn.ElementStyle>
                    <Style>
                        <!--EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/-->
                    </Style>
                </DataGridHyperlinkColumn.ElementStyle>
            </DataGridHyperlinkColumn>
        </DataGrid.Columns>
    </DataGrid>

C# 代码如下.

 public MainWindow()
 {
    InitializeComponent();

    List<User> users = new List<User>();
    users.Add(new User() { WebSite = new Uri("http://www.google.com") });
    users.Add(new User() { WebSite = new Uri("http://www.yahoo.com") });
    users.Add(new User() { WebSite = new Uri("http://www.gmail.com") });
    //DgVisibility = Visibility.Hidden;
    //userslink = users;
    dgUsers.ItemsSource = users;

 }

 public class User
 {
     public Uri WebSite { get; set; }
 }

 private void DG_Hyperlink_Click(object sender, RoutedEventArgs e)
 {
     Hyperlink link = (Hyperlink)e.OriginalSource;
     Process.Start(new ProcessStartInfo(link.NavigateUri.AbsoluteUri));
 }

提前致谢.

推荐答案

复制自评论:

将文本列与仅包含文本的自定义模板一起使用,并通过交互触发器将鼠标单击绑定到处理程序.

Use text column with custom template containing text only, with interaction trigger to bind mouse click to a handler.

在 MouseOver 触发器上添加一个将 Foreground 设置为红色/蓝色的样式,以模仿 url 的外观和感觉.这基本上就是超链接控件的作用.

Add a style with Foreground set to red/blue on MouseOver trigger to mimic url look and feel. This is basically what Hyperlink control does.

这篇关于通过 C# WPF 表单中的代码在网络浏览器(IE 或 chrome)中打开超链接 URI(www.google.com)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:37