The content elements of the tabitem of the TabControl control, such as the DataGrid control, need to be concerned about the source of the event when handling the event, and when the event of the DataGrid needs to be processed, the event is passed to TabControl. The way to solve this problem is as follows:
Method One: Determine who triggered this event, according to the source of the event to do the corresponding processing
Private void tabcontrol_selectionchanged (object sender, SelectionChangedEventArgs e) { If is TabControl) { //dosomething }}
Method Two: Register the corresponding event for the DataGrid and other corresponding controls so that it no longer passes upward
Private void datagrid_selectionchanged (object sender, SelectionChangedEventArgs e) { True;}
Problem extension: Unable to set the current item dynamically ...
For example, if you want to use the TabItem content element in the DataGrid's double-click event,
void Datagrid_mousedoubleclick (object sender, MouseButtonEventArgs e) { // Do something }
If you do not add e.handled = true, and at this point we need to dynamically change the selection of TabControl, we will face such a problem, no matter how we change the value below, Its value always changes to the item page when we handle the double-click event.
this. Tabcontrol.selecteditem = Newtabitem;
WPF TabControl Control-event-related issues