如何处理嵌套的tabcontrol SelectionChanged事件?

时间:2018-10-24 13:58:23

标签: c# .net wpf event-handling

TabControl的嵌套如下:

MainWindow.XAML

<TabControl SelectionChanged="Details_SelectionChanged" SelectedIndex="1"  x:Name="Details" >
     <TabItem Header="STATUS"   x:Name="Tab_STATUS">

        <TabControl SelectionChanged="Details_SelectionChanged"  x:Name="StatusSubMenu" >
            <TabItem  Header="CPU" x:Name="Tab_STATUS_CPU">
            </TabItem>
            <TabItem  Header="IO" x:Name="Tab_STATUS_IO">
            </TabItem>
        </TabControl>

     </TabItem>

    <TabItem Header="SETTINGS"   x:Name="Tab_SETTINGS">
        <TabControl SelectionChanged="Details_SelectionChanged"  x:Name="SettingsSubMenu" >
            <TabItem  Header="CPU" x:Name="Tab_Settings_CPU">
            </TabItem>
            <TabItem  Header="IO" x:Name="Tab_Settings_IO">
            </TabItem>
        </TabControl>
    </TabItem>

</TabControl>


我有一个事件处理程序 Details_SelectionChanged

MainWindow.XAML.cs

private void Details_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {


    }


我需要识别每个嵌套的TabControlTabitem以便调用与之相关的各个函数。我该怎么做?请帮助。

1 个答案:

答案 0 :(得分:1)

尝试一下:

private void Details_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (IsLoaded)
    {
        TabItem tabItem = e.AddedItems[0] as TabItem;
        TabControl tabControl = sender as TabControl;
        tb.Text = "Selected" + tabItem.Name + " in control " + tabControl.Name;
        e.Handled = true;
    }
}