Click here to Skip to main content
15,920,513 members

Comments by Member 16239100 (Top 13 by date)

Member 16239100 25-Apr-24 14:27pm View    
I think the Algorithm will be like:

When content is not SCrolling
1) During Mouse Move Draw the rectangle for Selection and iterate over each row to check what rows are intersecting with rectangle to make them selected otherwise unselect the rows if rectangle is not over them.

When content is AutoScrolling:
1) Keep drawing the rectangle and Whatever rows are intersecting select it. But the rows which are going out of view for them Keep a Flag like contentISautscrolling so dont deselect them.

Is it the right way?

Member 16239100 25-Apr-24 12:43pm View    
Hi @Pete O'Hanlon how can i improve the solution which i have posted as answer. I think we need to change the selection Logic there as you have suggested. Here i am using RowMosueEnter events
to do the selection.
Member 16239100 25-Apr-24 12:33pm View    
Deleted
// Variables to store selection rectangle info
Point startPoint;
Rectangle selectionRectangle;

// Mouse down event handler
private void DataGrid_MouseDown(object sender, MouseEventArgs e)
{
startPoint = e.GetPosition(null);

// Initialize the selection rectangle
selectionRectangle = new Rectangle
{
Stroke = Brushes.Black,
StrokeThickness = 1,
Opacity = 0.5
};

Canvas.SetLeft(selectionRectangle, startPoint.X);
Canvas.SetTop(selectionRectangle, startPoint.Y);

canvas.Children.Add(selectionRectangle);
}

// Mouse move event handler
private void DataGrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point endPoint = e.GetPosition(null);

double x = Math.Min(endPoint.X, startPoint.X);
double y = Math.Min(endPoint.Y, startPoint.Y);

double width = Math.Abs(endPoint.X - startPoint.X);
double height = Math.Abs(endPoint.Y - startPoint.Y);

selectionRectangle.Width = width;
selectionRectangle.Height = height;

Canvas.SetLeft(selectionRectangle, x);
Canvas.SetTop(selectionRectangle, y);
}
}

// Mouse up event handler
private void DataGrid_MouseUp(object sender, MouseEventArgs e)
{
canvas.Children.Remove(selectionRectangle);

// Now iterate through your DataGrid cells and check for selection
foreach (DataGridRow row in dataGrid.Items)
{
foreach (DataGridColumn column in dataGrid.Columns)
{
var cell = GetCell(dataGrid, row.Item, column);
Point cellTopLeft = cell.PointToScreen(new Point(0, 0));
Point cellBottomRight = cell.PointToScreen(new Point(cell.ActualWidth, cell.ActualHeight));

// Check if the cell is within the selection rectangle
if (IsCellWithinSelection(cellTopLeft, cellBottomRight))
{
// Perform selection logic here
}
}
}
}

// Helper method to check if a cell is within the selection rectangle
private bool IsCellWithinSelection(Point topLeft, Point bottomRight)
{
double selectionLeft = Canvas.GetLeft(selectionRectangle);
double selectionTop = Canvas.GetTop(selectionRectangle);
double selectionRight = selectionLeft + selectionRectangle.Width;
double selectionBottom = selectionTop + selectionRectangle.Height;

return topLeft.X >= selectionLeft && topLeft.Y >= selectionTop
&& bottomRight.X <= selectionRight && bottomRight.Y <= selectionBottom;
}

// Helper method to get a DataGridCell from row and column
private DataGridCell GetCell(DataGrid grid, object item, DataGridColumn column)
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item);
if (row != null)
{
int columnIndex = grid.Columns.IndexOf(column);
if (columnIndex > -1)
{
DataGridCellsPresenter presenter = GetVisualChild<datagridcellspresenter>(row);
return (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
}
}
return null;
}
Just to took the idea of code i took from AI and i think we should do selection during mouse move.
Member 16239100 25-Apr-24 11:17am View    
Here how to handle the scrolling case where on autoscroll rectangle will not over the rows which are not visible as they scrolldown. How to keep them selected. That time the rectangle will move alongwith scollable content.
Member 16239100 11-Apr-24 9:33am View    
it's not like your thoughts are not useful but i am not able to analyse like when to redraw the rectangle with fixed starting point and when to not. Can you please elaborate clearly. And since I am very new to this technology sometimes I might not understand what you are trying to convey.
Thanks for your time 😊