如何判断单击哪一行DataGrid?

时间:2017-06-17 11:58:54

标签: c# wpf xaml datagrid

我有一个<b> <div style='color:darkblue'> A Simple Form </div> </b> <i> <div> form fundametals </div> </i> <form> <fieldset> <legend>Customer Info:</legend> Name: <input type="text" name="name" placeholder="Enter Your Name"><br> Telephone: <input type="tel" name="tel" placeholder="Pattern: 1-234-567-8910" ><br> Email Address: <input type="email" name="email" placeholder="Enter Your Email Address"> </fieldset> <fieldset> <legend> Book:</legend> <input type="text"> Quantity(Maximum 5)<br> <input type="number" name="quantity" max="5"> <br> </fieldset> <br> <input type="button" name="submit" value="Submit"> </form>,其中填充了一个对象列表。 它有一个DataGrid事件。

我试图找出点击MouseDoubleClick的确切行

到目前为止,我已经尝试过这个:

DataGrid

遗憾地得到了private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) { DataGridRow Row = sender as DataGridRow; int RowNumber = Row.GetIndex(); //dostuff with RowNumber ; }

xaml表示完整性:

System.NullReferenceException

1 个答案:

答案 0 :(得分:1)

您获得null异常,因为它是发送事件的网格(DataGrid),您尝试将其转换/转换为DataGridRow。您可能希望DataGrid.SelectedIndex指示网格的SelectedItem或当前选定行的索引。请注意,该索引为零索引。

private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
    var dataGrid = sender as DataGrid;
    if (dataGrid != null) {
        var index = dataGrid.SelectedIndex;
        //dostuff with index
    }
}