Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the datagrid rows to be selected using a selection rectangle like we do in win File explorer
Can anyone please tell me how to make this kind of behaviour in our own WPF app. Right now i am making a File explorer kind of behaviour using the WPF datagrid. Can someone please help me with the logic or post some code.

What I have tried:

Please help me how to start and what logic they have used?
Posted

1 solution

There are two parts to this problem if you are keeping it simple, and assuming that your datagrid is entirely viewable in one screen. The first thing you need to consider is how to draw a selection rectangle. There's an article here[^] that should be relatively easy to adapt to handle drawing the drag rectangle.

The second part follows the first part. The selection rectangle gives you the bounds that define, in screen co-ordinates. What you could do is iterate through each cell and see if the coordinates for the cell sit inside that rectangle. You are looking to translate the coordinates using something like
C#
Point screenCoordinates = cell.PointToScreen(new Point(0, 0));
The value in screenCoordinates should give you the physical location of that cell (at the top left hand corner of the cell). If you are looking to see if the whole cell is in the selection rectangle, you will need to repeat for the bottom right hand corner.
 
Share this answer
 
Comments
Member 16239100 25-Apr-24 11:17am    
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.
Pete O'Hanlon 25-Apr-24 12:15pm    
If you have a Selected property on each row of your grid, when new ones scroll into view (as long as you are still drawing your rectangle), don't set the Selected property to false. That's the easiest part of the whole thing.
Member 16239100 25-Apr-24 12:43pm    
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 14:27pm    
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?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900