WPF在TabItems问题之间导航

时间:2015-10-14 23:13:47

标签: wpf tabitem

我在TabItem中有这个DataGrid:

        <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>

我希望当我双击DataGrid时会显示另一个标签,所以我使用了这个方法:

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

但没有任何反应。 当我按下&#39; btnJump&#39;按钮此事件触发:

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

这里的标签真的显示出来了。

有什么区别? 我该如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您怀疑它是焦点/其他行为问题,请尝试将选择调度到以后:

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