DataGrid-在当前项目上运行事件处理程序的键 [英] DataGrid - key to run event handler on current item

查看:54
本文介绍了DataGrid-在当前项目上运行事件处理程序的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小型WPF程序的源代码如下所示。它在 DataGrid 中的 c:\windows 下列出目录。该名称是可以单击以在资源管理器中打开目录的链接。

The source to a small WPF program is shown below. It lists the directories under c:\windows in a DataGrid. The name is a link that can be clicked to open the directory in Explorer.

(这只是说明问题的概念证明程序。)

(This is just a proof of concept program to illustrate the question.)

这是它的样子:

而不是只能单击在运行打开操作的链接上,我还想对其进行设置,以便用户在突出显示一行时可以按 o 键以运行

Instead of only being able to click on the link to run the open action, I'd also like to set it up so that the user can press the o key when a row is highlighted in order to run the open action.

设置它的好方法是什么?请注意,该程序主要是在C#中指定的,而不是XAML,因此,请尽可能在C#中发布解决方案。但是,如有必要,也欢迎使用XAML答案!

What's a good way to set this up? Note that the program is primarily specified in C# as opposed to XAML so if possible, please post your solution in C#. However, if necessary, XAML answers are welcome too!

MainWindow.xaml

<Window x:Class="WpfFilesDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfFilesDataGrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

    </Grid>
</Window>

MainWindow.xaml.cs

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

namespace WpfFilesDataGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var data_grid = new DataGrid()
            {
                IsReadOnly = true,
                AutoGenerateColumns = false,
                ItemsSource = new DirectoryInfo(@"c:\windows").GetDirectories()
            };

            {
                var setter = new EventSetter()
                {
                    Event = Hyperlink.ClickEvent,
                    Handler = (RoutedEventHandler)((sender, e) => 
                    {
                        System.Diagnostics.Process.Start((data_grid.SelectedItem as DirectoryInfo).FullName);
                    })
                };

                var style = new Style();

                style.Setters.Add(setter);

                var col = new DataGridHyperlinkColumn()
                {
                    Header = "FullName",
                    Binding = new Binding("FullName"),
                    ElementStyle = style
                };

                data_grid.Columns.Add(col);
            }

            data_grid.Columns.Add(new DataGridTextColumn()
            {
                Header = "CreationTime",
                Binding = new Binding("CreationTime")
            });

            var dock_panel = new DockPanel();

            dock_panel.Children.Add(data_grid);

            Content = dock_panel;
        }
    }
}


推荐答案

例如,您可以处理 PreviewKeyDown 事件:

You could for example handle the PreviewKeyDown event:

data_grid.PreviewKeyDown += (s, e) =>
{
    if(e.Key == Key.O && data_grid.SelectedItem is DirectoryInfo di)
        System.Diagnostics.Process.Start(di.FullName);
};

这篇关于DataGrid-在当前项目上运行事件处理程序的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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