Simple trick for getting right the DataGrid’s SelectedItem

I have been developing a Silverlight business application and one of the problems I have found is the “inconsistency” of the DataGrid… at least of some of its behaviors, when I click on a row, I expect that the SelectedItem (the clicked one) is set on the corresponding DataGrid property. 

But it does not. at least not “always”, lets be clear maybe I’m doing something wrong but this randomness on this behavior is driving me crazy, if I click on a Grid row, it should select it and mark the SelectedItem on the corresponding property, right? 

I have been doing so in the MouseLeftButtonUp of the DataGrid, so that’s what I expected but… sometimes it had it, sometimes not… bummer! 

So I have been trying to detect why is this happening but could not discover it… maybe I’m wrong in any of the assumptins but meanwhile, I got a workaround that does really work :). 

The following code explains it all… 

void datagrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { 

  if (datagrid.SelectedItem == null){ 


      FrameworkElement fe = (e.OriginalSource as FrameworkElement); 


      FrameworkElement feCell = (fe.Parent as FrameworkElement); 


      var myData = feCell.DataContext; 



Basically I check first If I have the SelectedItem set, sometimes it is, so then I avoid doing the following trick.

The “trick” mainly gets the originalsource that is launching the event, that is the control inside the cell of the grid,  most of times it will be a TextBox. Second I get the Parent, with this I am getting to the Cell.

Note that if you have a complex structure you should set up a recursive function to go “up” until you get a reference for the cell.

From the cell you have the datacontext which is the item whe should have got from the SelectedItem property.  you should cast it to the inherent type and get any value you need from it.

Also, please note that the DataContext could be caught from the first element (OriginalSource) but it’s more ellegant to get it from the Cell, and if it is a complex custom cell this also asures that it gets the DataContext properly.

And yes, it works 100% 😉

Happy coding!

PS: If somebody knows what I have done wrong or why the DataGrid.SelectedItem Property does not return the clicked item, I would be happy to hear. 🙂

3 thoughts on “Simple trick for getting right the DataGrid’s SelectedItem

  1. Hi Jose,

    I have faced the same problem as above but not only with datagrid, but also with listbox and treeview while using them with MVVM as follows

    so i used following solution

    public class ListBoxEx : ListBox
    public event RoutedEventHandler SelectedItemChanged;
    public ListBoxEx()
    this.SelectionChanged += new SelectionChangedEventHandler(ListBoxEx_SelectionChanged);

    void ListBoxEx_SelectionChanged(object sender, SelectionChangedEventArgs e)
    IList selectedItems = this.SelectedItems;

    if (this.SelectedItemsChanged != null)
    this.SelectedItemsChanged(this, new RoutedEventArgs());

    I think its a problem with Selector class.
    With Silverlight I some times feel MS is not maintaining its standard. This little bug is very annoying.


  2. Sure, that is one of these little “devils” that you have to know & control… and that can drive you crazy and make you loose lots of time… :(.

    Thanks for sharing!! 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s