使用 WPF DataGridHyperLinkColumn Items 打开 Windows 资源管理器并打开文件 [英] Using WPF DataGridHyperLinkColumn Items to open Windows Explorer and open files

查看:23
本文介绍了使用 WPF DataGridHyperLinkColumn Items 打开 Windows 资源管理器并打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下目标:

创建一个具有 2 列的 WPF DataGrid:

Create a WPF DataGrid that have 2 columns:

第一个将以超链接样式显示目录路径的项目.单击超链接将在项目指定的路径中打开 Windows 资源管理器.

The first will have items showing paths to directories, in a hyperlink style. Clicking on a hyperlink will open Windows Explorer in the path specified by the item.

第二个项目将以超链接样式显示文件路径.单击超链接将使用 Windows 定义的默认应用程序启动文件.

The second will have items showing paths to files, in a hyperlink style. Clicking on a hyperlink will launch the file, with the default application defined by Windows.

我不知道这是否是正确的选择,但我将 DataGridHyperlinkColumn 添加到我的 DataGrid 中.一个问题是添加不涉及互联网位置的 Uri 项目.另一个问题是以不打开网络浏览器的方式处理点击.

I don't know if it's the right choice, but I added DataGridHyperlinkColumn's to my DataGrid. One problem was to add Uri items that do not refer to an internet locations. Another problem was to handle the clicks in a way that does not open a web browser.

有人可以帮忙吗?

推荐答案

这普遍适用:

<DataGridHyperlinkColumn Binding="{Binding Link}">
    <DataGridHyperlinkColumn.ElementStyle>
        <Style>
            <EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/>
        </Style>
    </DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>

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

如果 URI 指向一个网站,它将使用默认的网络浏览器打开,如果它是一个文件夹,它将在资源管理器中打开,如果它是一个文件,它将使用与其关联的默认应用程序打开.

If the URI points a website it will be opened with the default web-browser, if it is a folder it will be opened in explorer, if it is a file it will be opened with the default application associated with it.

要将其用于自动生成的列,您的属性需要属于 Uri 类型,以便生成 DataGridHyperlinkColumn.然后,您可以通过将样式放置在 DataGrid.Resources 中来连接事件:

To use this for autogenerated columns your property needs to be of type Uri so a DataGridHyperlinkColumn is generated. You then can hook up the event by placing the style in the DataGrid.Resources:

<DataGrid.Resources>
    <Style TargetType="Hyperlink">
        <EventSetter Event="Click" Handler="DG_Hyperlink_Click"/>
    </Style>
</DataGrid.Resources>

这篇关于使用 WPF DataGridHyperLinkColumn Items 打开 Windows 资源管理器并打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆