16,022,536 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Visual Basic questions
View Javascript questions
View .NET questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
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 😊
Member 16239100
11-Apr-24 9:06am
View
I don't know what are you trying to say. Can you tell me did you really understood the problem or I have to rephrase the question for better understanding?
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.
Member 16239100
11-Apr-24 8:27am
View
here if you check on left click im keeping the startpoint of rectangle fixed and ending point is keep on changing during mouse move.
can you check my SelectItemsInRectangle function. I think if i everytime redraw rectangle by changing the start point then the rows below it will deselect or can you have some please suggest some better logic.
private void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//DependencyObject dep = (DependencyObject)e.OriginalSource;
//// Check if the mouse click is on the headers or scroll bars
//while (dep != null && dep != FileDataGrid)
//{
// if (dep is DataGridColumnHeader || dep is ScrollBar)
// {
// // Clicked on headers or scroll bars, do not initiate selection
// return;
// }
// dep = VisualTreeHelper.GetParent(dep);
//}
isleftbuttonpressed = true;
isSelecting = true;
selectionStartPoint = e.GetPosition(FileDataGrid);
adorner = new SelectionAdorner(FileDataGrid, selectionStartPoint);
var adornerLayer = AdornerLayer.GetAdornerLayer(FileDataGrid);
adornerLayer.Add(adorner);
adorner.SetStartPoint(selectionStartPoint);
}
private void DataGrid_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
isleftbuttonpressed = true;
e.Handled = false;
}
if (isSelecting && adorner != null)
{
adorner.SetEndPoint(e.GetPosition(FileDataGrid));
var selectionRect = adorner.GetSelectionRect();
SelectItemsInRectangle(selectionRect, FileDataGrid, sender, e);
}
public Rect GetSelectionRect()
{
// double height = Math.Abs(endPoint.Y - startPoint.Y); // Calculate height
// selectionHeights.Add(height);
return new Rect(startPoint, endPoint);
}
private void SelectItemsInRectangle(Rect selectionRect, DataGrid dataGrid, object sender, MouseEventArgs e)
{
foreach (var item in dataGrid.Items)
{
var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(item);
if (row != null)
{
var rowBounds = GetRowBoundsRelativeToDataGrid(row, dataGrid);
// Check if the row intersects with the selection rectangle
if (selectionRect.IntersectsWith(rowBounds))
{
// Select the item and add it to the collection of selected items
row.IsSelected = true;
}
else
{
// If the item was previously selected but is not in the selection rectangle, deselect it
row.IsSelected = false;
}
}
}
}
Member 16239100
11-Apr-24 8:26am
View
Deleted
here if you check on left click im keeping the startpoint of rectangle fixed and on mouse move and ending point is keep on changing during mouse move.
can you check my SelectItemsInRectangle function. I think if i everytime redraw rectangle by changing the start point then the rows below it will deselect or can you have some please suggest some better logic.
private void DataGrid_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
isleftbuttonpressed = true;
e.Handled = false;
}
if (isSelecting && adorner != null)
{
adorner.SetEndPoint(e.GetPosition(FileDataGrid));
var selectionRect = adorner.GetSelectionRect();
SelectItemsInRectangle(selectionRect, FileDataGrid, sender, e);
}
public Rect GetSelectionRect()
{
// double height = Math.Abs(endPoint.Y - startPoint.Y); // Calculate height
// selectionHeights.Add(height);
return new Rect(startPoint, endPoint);
}
private void SelectItemsInRectangle(Rect selectionRect, DataGrid dataGrid, object sender, MouseEventArgs e)
{
foreach (var item in dataGrid.Items)
{
var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(item);
if (row != null)
{
var rowBounds = GetRowBoundsRelativeToDataGrid(row, dataGrid);
// Check if the row intersects with the selection rectangle
if (selectionRect.IntersectsWith(rowBounds))
{
// Select the item and add it to the collection of selected items
row.IsSelected = true;
}
else
{
// If the item was previously selected but is not in the selection rectangle, deselect it
row.IsSelected = false;
}
}
}
}
Member 16239100
11-Apr-24 8:23am
View
Deleted
here if you check on left click im keeping the startpoint of rectangle fixed and on mouse move and ending point is keep on changing during mouse move.
can you check my SelectItemsInRectangle function. I think if i everytime redraw rectangle by changing the start point then the rows below it will deselect or can you have some please suggest some better logic.
private void DataGrid_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
isleftbuttonpressed = true;
e.Handled = false;
}
if (isSelecting && adorner != null)
{
adorner.SetEndPoint(e.GetPosition(FileDataGrid));
var selectionRect = adorner.GetSelectionRect();
SelectItemsInRectangle(selectionRect, FileDataGrid, sender, e);
}
public Rect GetSelectionRect()
{
// double height = Math.Abs(endPoint.Y - startPoint.Y); // Calculate height
// selectionHeights.Add(height);
return new Rect(startPoint, endPoint);
}
private void SelectItemsInRectangle(Rect selectionRect, DataGrid dataGrid, object sender, MouseEventArgs e)
{
foreach (var item in dataGrid.Items)
{
var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(item);
if (row != null)
{
var rowBounds = GetRowBoundsRelativeToDataGrid(row, dataGrid);
// Check if the row intersects with the selection rectangle
if (selectionRect.IntersectsWith(rowBounds))
{
// Select the item and add it to the collection of selected items
row.IsSelected = true;
}
else
{
// If the item was previously selected but is not in the selection rectangle, deselect it
row.IsSelected = false;
}
}
}
}
Member 16239100
11-Apr-24 8:23am
View
Deleted
private void DataGrid_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
isleftbuttonpressed = true;
e.Handled = false;
}
if (isSelecting && adorner != null)
{
adorner.SetEndPoint(e.GetPosition(FileDataGrid));
var selectionRect = adorner.GetSelectionRect();
SelectItemsInRectangle(selectionRect, FileDataGrid, sender, e);
}
public Rect GetSelectionRect()
{
// double height = Math.Abs(endPoint.Y - startPoint.Y); // Calculate height
// selectionHeights.Add(height);
return new Rect(startPoint, endPoint);
}
private void SelectItemsInRectangle(Rect selectionRect, DataGrid dataGrid, object sender, MouseEventArgs e)
{
foreach (var item in dataGrid.Items)
{
var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(item);
if (row != null)
{
var rowBounds = GetRowBoundsRelativeToDataGrid(row, dataGrid);
// Check if the row intersects with the selection rectangle
if (selectionRect.IntersectsWith(rowBounds))
{
// Select the item and add it to the collection of selected items
row.IsSelected = true;
}
else
{
// If the item was previously selected but is not in the selection rectangle, deselect it
row.IsSelected = false;
}
}
}
}
here if you check on left click im keeping the startpoint of rectangle fixed and on mouse move and ending point is keep on changing during mouse move.
can you check my SelectItemsInRectangle function. I think if i everytime redraw rectangle by changing the start point then the rows below it will deselect or can you have some please suggest some better logic.
Member 16239100
11-Apr-24 7:54am
View
so on mouse move i just need to update the end point or do i need to redraw the rectangle?
Member 16239100
11-Apr-24 6:01am
View
No actually if you see win explorer use a selection rectangle to select the files and folders.
Member 16239100
11-Apr-24 5:31am
View
Hi, @Jo_vb.net i think you did not understand the problem. The problem is the selection rectangle is not the part of the datagrid, its an adorner which is selecting the datagrid rows by hovering over it like we do in windows file explorer. Son here what is happening, the rectangle will remain fixed at one position during scroll as its not the part of the datagrid scrollviewer and the datagrid rows content will scroll up and down. You can see the visual representation here
https://stackoverflow.com/questions/78304588/selection-rectangle-not-moving-along-with-wpf-datagrid-content-during-scroll
You can see in the pic that selection rectangle is above the rows and its like its in the air and the content below it will scroll up and down and it will remain fixed. But in windows file explorer if you see, its the part of datagrid and it will scroll with datagrid content. So is there any way i can fix this problem.
Show More