如何在WPF中处理TabItem单击事件?

pvc*_*pvc 18 c# wpf

在我的应用程序中我使用了WPF TabControl我想处理的click事件TabItem.我如何实现它?

d.m*_*ada 19

您可以通过向tabcontrol中每个tabitem的header属性添加标签来完成此操作.然后,您可以为标签设置事件.

XAML

<TabControl Height="100" HorizontalAlignment="Left" Name="tabControl1">
    <TabItem  Name="tabItem1">
        <TabItem.Header>
            <Label Content="tabItem1" 
                MouseLeftButtonDown="tabItem1_Clicked" 
                HorizontalAlignment="Stretch"/>
        </TabItem.Header>
        <Grid />
    </TabItem>
    <TabItem  Name="tabItem2">
        <TabItem.Header>
            <Label Content="tabItem2" 
                MouseLeftButtonDown="tabItem2_Clicked" 
                HorizontalAlignment="Stretch"/>
        </TabItem.Header>
        <Grid />
    </TabItem>
</TabControl>
Run Code Online (Sandbox Code Playgroud)

C#/ Code Behind

private void tabItem1_Clicked(object sender, MouseButtonEventArgs e)
{
    //DO SOMETHING
}

private void tabItem2_Clicked(object sender, MouseButtonEventArgs e)
{
    //DO SOMETHING
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


bAN*_*bAN 14

这是一个古老的问题,但我在相同的情况下找到答案.

我在TabControl(XAML)上使用SelectionChanged事件

<TabControl SelectionChanged="TabControl_SelectionChanged">
Run Code Online (Sandbox Code Playgroud)

代码背后(C#):

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //Stuff
}
Run Code Online (Sandbox Code Playgroud)

这不是点击他自己,而是为了让人耳目一新.

  • 这种类型的解决方案给我带来了一个问题,因为它似乎在tabcontrol中的控件更改选择时触发,而不仅仅是tabcontrol本身的选择。 (3认同)

小智 11

您可以在TabControl中使用SelectionChanged事件并使用switch case执行您喜欢的任何操作.

// XAML代码

    <TabControl SelectionChanged="TabControl_SelectionChanged">
        <TabItem Header="Item1"></TabItem>
        <TabItem Header="Item2"></TabItem>
        <TabItem Header="Item3"></TabItem>
    </TabControl>
Run Code Online (Sandbox Code Playgroud)

//代码背后

    private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string tabItem = ((sender as TabControl).SelectedItem as TabItem).Header as string;

        switch(tabItem)
        {
            case "Item1":
                break;

            case "Item2":
                break;

            case "Item3":
                break;

            default:
                return;
        }
    }
Run Code Online (Sandbox Code Playgroud)


H.B*_*.B. 1

将标题包裹在无模板按钮中。

如果您使用 ItemsSource:

<TabControl ItemsSource="{Binding Data}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <Button Click="Tab_Click">
                <Button.Template>
                    <ControlTemplate>
                        <ContentPresenter />
                    </ControlTemplate>
                </Button.Template>
                <Button.Content>
                    <!-- Actual header goes here -->
                </Button.Content>
            </Button>
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>
Run Code Online (Sandbox Code Playgroud)

如果您有静态内容,您可以立即将其插入标题中:

<TabControl>
    <TabItem>
        <TabItem.Header>
            <Button Click="Tab_Click">
                <Button.Template>
                    <ControlTemplate>
                        <ContentPresenter />
                    </ControlTemplate>
                </Button.Template>
                <Button.Content>
                    <!-- Actual header goes here -->
                </Button.Content>
            </Button>
        </TabItem.Header>
    </TabItem>
</TabControl>
Run Code Online (Sandbox Code Playgroud)