Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# datagrid单元格行单击事件_C#_Wpf_Datagrid - Fatal编程技术网

C# datagrid单元格行单击事件

C# datagrid单元格行单击事件,c#,wpf,datagrid,C#,Wpf,Datagrid,我的WPFDataGrid如下所示 加价 <DataGrid x:Name="Processes" AutoGenerateColumns="False" ItemsSource="{Binding Path=ProcessesBinding}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="807" Margin="13,32,0,0"> <DataGri

我的WPF
DataGrid
如下所示

加价

    <DataGrid x:Name="Processes" AutoGenerateColumns="False" ItemsSource="{Binding Path=ProcessesBinding}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="807" Margin="13,32,0,0">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Id}" Header="ID" Width="50" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Friendlyname}" Header="Name" Width="200" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Process}" Header="Process" Width="180" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Status}" Header="Status" Width="180" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Autostart}" Header="Auto Start" Width="100" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Autorestart}" Header="Auto Restart" Width="100" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
它是如何应用的

Processes.ItemsSource = dataTable.DefaultView;
我已经在网上搜索过了,但是,我无法推断出我需要什么

我想要什么:无论我在哪里单击第1行(
ID13
),都会运行一个事件。我唯一需要的是单击的行的
ID
,这样我就可以进一步处理“请求”

这就是我隐约想到的

private void CellClick(int ID) { // show new window with id
}

有一个名为
MouseDoubleClick
datagrid
事件。您可以这样使用它来获取双击的行的整个对象

private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender != null)
            {
                DataGrid grid = sender as DataGrid;
                if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                {
                    DataGridRow dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
                    YourClass obj = dgr.Item as YourClass;
                    this.selectedIndex = grid.SelectedIndex;
                    int id = obj.ID;
                }
            }
        }
因为我不知道你的班名,所以你可以用你的班名替换
YourClass


基本上,这段代码的作用是检测
Datagrid
上的双击事件。然后检查事件是否在
DataGridRow
上执行。然后它将该行的项类型转换为您定义的类。并且该行的所有信息都将填充到接收类型转换对象的对象中。

如果不使用MVVM方法,而只是使用EventHandlers,则可以添加一个处理程序来更改所选单元格:

<DataGrid SelectedCellsChanged="yourGrid_SelectedCellsChanged" ...>

除了
@UmairFarooq
所建议的之外,如果您在单元格中有一个控件(例如按钮),另一种方法可以如下所示:

基本上,单元格中的控件将继承行数据对象的
DataContext
。让我们称之为MyObject。所以,MyObject.ID就是您想要的

private void Button_Click(object sender, MouseButtonEventArgs e)
{
     MyObject obj = ((FrameworkElement)sender).DataContext as MyObject;
    //Now you can do whatever you wanted to do with MyObject.ID
}
 private void yourGrid_SelectedCellsChanged(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
    {
        // retrieve the id here, like in the answer above
    }
private void Button_Click(object sender, MouseButtonEventArgs e)
{
     MyObject obj = ((FrameworkElement)sender).DataContext as MyObject;
    //Now you can do whatever you wanted to do with MyObject.ID
}