Get Grid Cell by mouse click

后端 未结 4 1392
逝去的感伤
逝去的感伤 2020-12-20 14:29

I have a WPF Grid which is divided into 3 rows and 3 columns, i wasn\'t able to find a way of getting the row and column number of mouse click on the net, ohh and if it is p

4条回答
  •  情歌与酒
    2020-12-20 15:05

    It's answered here: Ways to identify which cell was clicked on WPF Grid?

    I've never used WPF Grid before, though using that link above as an example I think something like this should do it:

    Add this to your Initialize method:

    this.GridCtrl.MouseDown += new MouseButtonEventHandler(GridCtrl_MouseDown);
    

    And add new method to handle the event:

    private void GridCtrl_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (sender != null)
        {
            Grid _grid = sender as Grid;
            int _row = (int)_grid.GetValue(Grid.RowProperty);
            int _column = (int)_grid.GetValue(Grid.ColumnProperty);
            MessageBox.Show(string.Format("Grid clicked at column {0}, row {1}", _column, _row));
        }
    }
    

提交回复
热议问题