DataGrid Focus and SelectedIndex?

It is not easy to understand this concept. If you click a line on WPF DataGrid

, it is colored with SystemColors.HighlightBrushKey

and has "focus".

If you select a different control, that string no longer has SystemColors.HighlightBrushKey

, but instead a different color (not sure what SystemColor is). However, it still counts DataGrid

selectedItem

.

I would expect that if the DataGrid loses focus then it selectedIndex

will always be -1 and selectedItem

how null

. What is the purpose of this behavior, and is there an easy way to disable it?

+3


source to share


1 answer


Suppose you have a DataGrid and the "Do things with selected item" button is detached. How can you make it work if the grid is dropping the selection on lost focus? This is one simplified example, but in general I don't see how stubborn a selection condition is that the user has done badly.



Disabling this seems pretty straightforward, just do SelectedItems.Clear()

on LostFocus

. You can create your own DataGrid subclass and place it there, so you don't have to write it in every grid. You will have to implement something a little more complex to make it work with multi-selects.

0


source







All Articles