Finding regions of DataGrid.SelectedCells

Sep 14, 2008 at 8:48 PM
Is there a simple way to get the selected regions with the CTP DataGrid?
 I  need a list of rectangles. where each rectangle represents one selected region of cells.

The CTP version of DataGrid only exposes SelectedCells as an IList<DataGridCellInfo> and goes out of its way to hide the SelectedCellsCollection.
This really makes customized drag and drop clumsy since what you want to know is the selected regions and that information is hidden.
You wind up having the build up the regions information yourself. 
It can fairly expensive to build the list and can be prone to bugs when  you consider multiselection.

Will SelectedCellsCollection get exposed out in a future version?

Sep 18, 2008 at 4:27 AM
What kind of scenario are you trying to implement for your customized drag and drop?  The thing about exposing the SelectedCellsCollection is that it is tied to a lot of internal implementation detail that could possible change in the future.  Understanding your scenario may be helpful in understanding what we can do to enable a better development experience.
Sep 24, 2008 at 3:58 PM
I too wish to implement a drag and drop scenario. My DataGrid is essentially a list of 'TennisMatch' objects. A column of my DataGrid represents a 'Player' object and just displays the Player's 'Name' property. I also have a listView containing a list of Players. I intend to drag a Player from my listView to a cell in the player column of my DataGrid.

I haven't gotten far in my attempt to implement this yet, but I thought you might appreciate the feedback.
Dec 9, 2009 at 5:35 AM
Edited Dec 9, 2009 at 5:38 AM

My scenario is implementing a custom paste routine where I need to make sure the shape of data being pasted matches that of the grid selection.

So, if someone copies a 2x2 square of cells from Excel and pastes this into our grid - which has a selection of cells equal to 4x1 - I need to know it's not the same shape and therefore something needs to be done.

I can only do this by using the Height/Width properties of the SelectedCellsCollection.

Dec 10, 2009 at 5:14 AM
Edited Dec 10, 2009 at 5:24 AM

To implement my scenario (previous post) and make it testable I made changes as below.

Remove everything 'internal' and conform SelectedCellsSelection to an interface (so I could mock it out for testing).

If you want to set up a valid SelectedCellsCollection object for unit-testing, without using mocks, then have a nice day (or few days) and let me know how it goes ;-)

A side question: Is it true then, that no one else who writes grid selection code, has a need to unit-test any of it?

It boils my blood to find libraries with code that is unnecessarily difficult to incorporate into valid unit-tests - use of internal and sealed and pointless private constructors etc. Why the default is not to assume that developers might have a valid reason to access this code, I really don't understand.

public interface ISelectedCellsCollection
        bool GetSelectionRange(
            out int minColumnDisplayIndex, out int maxColumnDisplayIndex,
            out int minRowIndex, out int maxRowIndex);

        int Count { get; }

    public sealed class SelectedCellsCollection : VirtualizedCellInfoCollection, ISelectedCellsCollection