WPF Datagrid & SelectedItem vs Selected Cells

Jun 1, 2009 at 4:01 PM

hi,

first off - thanks for the toolkit; to my mind the best datagrid product there is out there. i am having one little issue with it however, and i don't really understand it at all.

background:

I have a collection of objects, and they have a settable property "Sequence". The collection is ordered via sequence. I have the requirement that the user be able to use drag & drop to set the sequence. So my code on PreviewMouseMove effectively just checks that the mouse left button is pressed, and then shoves the datagrid's selected item into a dataobject and calls the DoDragDrop method.

On drop - I get the data, find the row at the relevant position, and set the sequence of the dropped object to the row's index + 1 - then the underlying collection will sort out the other sequences (i.e. if i'm moving object 1 to sequence 5, then objects 2, 3, 4 and 5 need to decrement their own sequences to keep parity) and sort itself. This all works fine...

... until! If I were to select a row, get into edit mode (doesn't have to change anything, as long as the text is selected), then move that row using the drag & drop, then select another item - it all falls down!

The row I just moved cannot be selected again - well, this isn't entirely true - it's never painted as being selected again, I can still drag and drop & edit etc. When debugging, if I was to move it again, handling the drop event shows the SelectedItem as not being the same item as SelectedCells[0].Item - the two are different. SelectedItem is the one I'm expecting (as in the one I've just moved and will not be painted as selected), and the SelectedCells is the currently painted cells.

This only happens to ONE item at a time - so if I am to perform a drag & drop on a different item, without doing the selecting first - then everything goes back to normal. If I am to do the selection first and then try and move it - it is that item that can't be selected and the original is fine.

I know this is weird, and is going to be difficult to offer any kind of solution since it's kind of specific circumstance - but this is really doing my head in! I can knock up a replica if required.

ALSO - and this isn't so much specific to the datagrid... but on the handle mouse move event - I have to check the Keyboard.Focused element to see if it's a textbox - and if it is a textbox, ensure that there is no text selected - otherwise don't perform the Drag/Drop. The reason for this is - if I attempt to call DragDrop.DoDragDrop(blah) whilst the cell has a textbox with its text selected, I get a Dispatcher processing suspended exception. Any ideas why this is? Is this to do with the way the textbox handles the selected text event? I don't think it's specific to the grid since this occurs on other grids (as I was trying to fix that!). The only reason why I mention it - could (and I can't see how) this line of code actually affect what's going on?

 

 

 

 

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

private

</font></font><font size="2" color="#0000ff">

 

</font>

void ItemGridPreviewMouseMove(object sender, MouseEventArgs e)

{

<font size="2">

 

</font>

 

TextBox focused = Keyboard.FocusedElement as TextBox;<font size="2">

 

</font>

 

if (focused != null && (focused.SelectionStart != 0 || focused.SelectionLength > 0))

{

<font size="2">

 

</font>

 

return;

}

<font size="2">

 

</font>

 

if (e.LeftButton == MouseButtonState.Pressed)

{

 

 

 

<font size="2" color="#2b91af"><font size="2" color="#2b91af">

DragDrop

</font></font><font size="2" color="#2b91af">

 

</font>

.DoDragDrop(this.itemGrid,new DataObject(format, this.itemGrid.SelectedItem),DragDropEffects.Move);

}

}

Any suggestions / ideas / pointers would be extremely helpful - as I said, I'm tearing my hair out - the disparity between SelectedCells & SelectedItem is completely weird and I can't see what could have caused it. Calling Items.RefreshItems() fixes it - but ONLY until the next processing happens (I tried calling it through the immediate window on the event handlers, and it does work, for about a nanosecond, before reverting back).

Looking at the code, I can't see why the SelectedCells would be different - and ONLY after a selection has been done, and then a move attempt.

 

Thanks for your time!