Search code examples
wpftabitem

WPF navigating between TabItems issue


I have this DataGrid in TabItem:

        <TabControl x:Name="MainTab" FontSize="12" IsSynchronizedWithCurrentItem="True" SelectionChanged="MainTab_SelectionChanged">
    <TabItem x:Name="tabAddDocs" Header=Add Docs"></TabItem>
    <TabItem x:Name="tabEmpList" Header="Employee List">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="70*"/>
                <ColumnDefinition Width="30*"/>
            </Grid.ColumnDefinitions>
            <DataGrid x:Name="empGrid" HorizontalAlignment="Left" VerticalAlignment="Top"  
                  SelectionMode="Single" AlternatingRowBackground="#FFCDCDCD" AutoGenerateColumns="True" 
                  AutoGeneratingColumn="empGrid_AutoGeneratingColumn" FlowDirection="RightToLeft" 
                  FontSize="14" IsReadOnly="True" MouseDoubleClick="empGrid_MouseDoubleClick" 
                  Loaded="empGrid_Loaded" Focusable="False" IsEnabled="False"/>
            <Button Grid.Column="1" Name="btnJump" Click="btnJump_Click">Jump</Button>
        </Grid>
    </TabItem>
</TabControl>

I want that when i double click on the DataGrid the other tab will be shown, so i used this method:

private void empGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        tabAddDocs.IsSelected = true;
    }

But nothing happens. when I press the 'btnJump' Button this event fires:

        private void btnJump_Click(object sender, RoutedEventArgs e)
    {
        tabAddDocs.IsSelected = true;
    }

And here the tab is really showing.

What are the differences? How can i solve this?

Thanks!


Solution

  • If you suspect it is a focus/other behavior issue, try dispatching the selection to later:

    private void btnJump_Click(object sender, RoutedEventArgs e)
    {
        Dispatcher.BeginInvoke((Action)delegate {tabAddDocs.IsSelected = true;}, 
                               DispatcherPriority.ApplicationIdle);
    }