MyTabControl.SelectedChanged Fires When Datagrid Is Selected Or Rebound

Mar 24, 2009 at 3:57 PM
Edited Mar 24, 2009 at 5:48 PM
I just wanted to mention an odd scenario I came across.  Add a tab control to your WPF application.  Then add a datagrid to a tab item.  Then put a break point in the SelectionChanged event of your tab control.  Bind some data to your datagrid.  Then run your application and select a row in the datagrid.  You will notice that the SelectionChanged event for the tab control fires when you select a row in the datagrid or if you rebind the datagrid. I think this may be by design.  The tooltip for the event states

Occurs when the selection of a changes.

Since a datagridrow is a primitive control it bubbles up to the SelectionChanged event of the tab control.  This may be by design but if this event isn't supposed to behave like the SelectedIndexChanged event in a windows application it would be nice if there was a SelectedIndexChanged event for a WPF tab control.

Mar 24, 2009 at 8:01 PM
You are correct that the SelectionChanged event is bubbling from the DataGrid through the TabControl on its way to the root of the tree. Since both the TabControl and the DataGrid derive from Selector, it can be confusing. Check the OriginalSource property of the event arguments, it should show where the event was originally raised. For instance, in the event handler on the TabControl, if OriginalSource == this, that means that the TabControl raised that event and not the DataGrid.

Mar 24, 2009 at 8:06 PM
Thaks for your help.  That works perfectly!  It just threw me off because I am used to the SelectionIndexChanged event in a typical windows application.

Jason V