Exemplo n.º 1
0
        private bool GetDataGridCellByPosition(Point pt, out DataGridRow dataGridRow,
                                               out DataGridColumn dataGridColumn, out DataGridCell dataGridCell,
                                               out object dataGridObject)
        {
            var elements = VisualTreeHelper.FindElementsInHostCoordinates(pt, this);

            dataGridRow    = null;
            dataGridCell   = null;
            dataGridColumn = null;
            dataGridObject = null;

            if (null == elements || elements.Count() == 0)
            {
                return(false);
            }

            var rowQuery = from gridRow in elements
                           where gridRow
                           is DataGridRow
                           select gridRow as DataGridRow;

            dataGridRow = rowQuery.FirstOrDefault();
            if (dataGridRow == null)
            {
                return(false);
            }

            dataGridObject = dataGridRow.DataContext;

            var cellQuery = from gridCell in elements
                            where gridCell is DataGridCell
                            select gridCell as DataGridCell;

            dataGridCell = cellQuery.FirstOrDefault();

            if (dataGridCell != null)
            {
                dataGridColumn = DataGridColumn.GetColumnContainingElement(dataGridCell);
            }

            //If we've got the row, return true -
            //sometimes the Column, DataContext could be null
            return(dataGridRow != null);
        }