Get Grid Cell by mouse click

后端 未结 4 1391
逝去的感伤
逝去的感伤 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:03

    I use something like this:

    private void DataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            // Check if the user double-clicked a grid row and not something else
            if (e.OriginalSource == null) return;
            var row = ItemsControl.ContainerFromElement((DataGrid)sender, e.OriginalSource as DependencyObject) as DataGridRow;
    
            // If so, go ahead and do my thing
            if (row != null)
            {
                var Item = (CLASS_YOU_USE_TO_BIND)DataGrid1.Items[row.GetIndex()];
    //Here you can work with Item, it is now the object of class you used in
    //DataGrid.DataSource
            }
    }
    
    0 讨论(0)
  • 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));
        }
    }
    
    0 讨论(0)
  • 2020-12-20 15:07

    just try this

    Grid.GetRow(NAME OF GRID)
    Grid.GetColumn(NAME OF GRID)
    
    0 讨论(0)
  • 2020-12-20 15:24

    Faced with the same problem I came up with this solution:

    XAML:

    <Grid Name="myGrid" Background="Transparent" PreviewMouseLeftButtonDown="OnPreviewMouseLeftButtonDown">
    

    NOTE: The Grid has to be given a background to raise the mouse down event, see: How to get a Grid to raise MouseDown events when no UIElemets in cells clicked?

    Code-behind:

    private void OnPreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if(e.ClickCount == 2) // for double-click, remove this condition if only want single click
        {
            var point = Mouse.GetPosition(myGrid);
    
            int row = 0;
            int col = 0;
            double accumulatedHeight = 0.0;
            double accumulatedWidth = 0.0;
    
            // calc row mouse was over
            foreach (var rowDefinition in myGrid.RowDefinitions)
            {
                accumulatedHeight += rowDefinition.ActualHeight;
                if (accumulatedHeight >= point.Y)
                    break;
                row++;
            }
    
            // calc col mouse was over
            foreach (var columnDefinition in myGrid.ColumnDefinitions)
            {
                accumulatedWidth += columnDefinition.ActualWidth;
                if (accumulatedWidth >= point.X)
                    break;
                col++;
            }
    
            // row and col now correspond Grid's RowDefinition and ColumnDefinition mouse was 
            // over when double clicked!
        }
    }
    
    0 讨论(0)
提交回复
热议问题