Cannot Switch TabItem from DoubleClick Event
I am trying to change the selected tab of my TabControl using the following code:
// Switch to configuration tab
tabControl.SelectedItem = configTab;
While it works just fine from within a ButtonClick handler, it does nothing from within a DataGrid DoubleClick handler on the same TabItem. I have set the debugger to this line and can see the SelectedItem property change, but the tab refuses to change.
EDIT: More Code
The tab control is defined as such:
<Grid>
开发者_Python百科 <TabControl Height="Auto" HorizontalAlignment="Stretch" Name="tabControl" VerticalAlignment="Stretch" Width="Auto" Padding="0" SelectionChanged="BuildSummary">
<TabItem Header="Configurations" Name="configTab">
...
</TabItem>
<TabItem Header="Temperature" Name="tempTab">
...
</TabItem>
<TabItem Header="Test List" Name="testTab">
...
</TabItem>
<TabItem Header="Summary" Name="summaryTab">
...
</TabItem>
</TabControl>
</Grid>
This is the working event:
private void Execute(object sender, RoutedEventArgs e)
{
// Switch to configuration tab
tabControl.SelectedItem = configTab;
}
This is the broken event:
private void DoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow row = ItemsControl.ContainerFromElement(
(DataGrid)sender, e.OriginalSource as DependencyObject)
as DataGridRow;
if (row == null)
return;
/* code here prepares the configuration tab */
...
// Switch to configuration tab
tabControl.SelectedItem = configTab;
}
What am I missing?
Use
e.handled = true;
to stop the click event from propagating up from the DataGrid to the current TabItem
~BalamBalam
Can you verify that your Double-Click event is being hit at all?
I believe the DataGrid
marks some of it's click events as handled, so they don't pass through to other events. It's possible you're using one of these events. If so, you'll have to switch to handling the double-click event at another level, such as on the DataGridCell
.
精彩评论