WPF中TabControl的TabChanged事件

发布于 2024-12-16 17:49:13 字数 57 浏览 1 评论 0 原文

我在 WPF 中有一个 TabControl。我想找到更改选项卡时发生的事件。这个活动的名称是什么?

I have a TabControl in WPF. I want to find an event that occurs when changing tabs. What is the name of this event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狠疯拽 2024-12-23 17:49:13

TabControl继承自 选择器,其中包含SelectionChanged 事件

<TabControl SelectionChanged="OnSelectionChanged" ... />

private void OnSelectionChanged(Object sender, SelectionChangedEventArgs args)
{
    var tc = sender as TabControl; //The sender is a type of TabControl...

    if (tc != null)
    {
        var item = tc.SelectedItem;

        //Do Stuff ...
    }
}

The TabControl inherits from a Selector which contains the SelectionChanged event.

<TabControl SelectionChanged="OnSelectionChanged" ... />

private void OnSelectionChanged(Object sender, SelectionChangedEventArgs args)
{
    var tc = sender as TabControl; //The sender is a type of TabControl...

    if (tc != null)
    {
        var item = tc.SelectedItem;

        //Do Stuff ...
    }
}
甜妞爱困 2024-12-23 17:49:13

我只想在这里补充一下我的观点。我将使用 @pratap k 的酷答案来做到这一点。

<TabControl x:Name="MyTab" SelectionChanged="TabControl_SelectionChanged">
    <TabItem x:Name="MyTabItem1" Header="One"/>
    <TabItem x:Name="MyTabItem2" Header="2"/>
    <TabItem x:Name="MyTabItem3" Header="Three"/>
</TabControl>

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (MyTabItem1 !=null && MyTabItem1.IsSelected)
    // do your staff
    if (MyTabItem2 !=null && MyTabItem2.IsSelected)
    // do your staff
    if (MyTabItem3 !=null && MyTabItem3.IsSelected)
    // do your staff
}

正如您所看到的,区别在于添加了对 NULL 的检查。

就是这样!

I just want to add my point here. And I will use cool answer of @pratap k to do it.

<TabControl x:Name="MyTab" SelectionChanged="TabControl_SelectionChanged">
    <TabItem x:Name="MyTabItem1" Header="One"/>
    <TabItem x:Name="MyTabItem2" Header="2"/>
    <TabItem x:Name="MyTabItem3" Header="Three"/>
</TabControl>

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (MyTabItem1 !=null && MyTabItem1.IsSelected)
    // do your staff
    if (MyTabItem2 !=null && MyTabItem2.IsSelected)
    // do your staff
    if (MyTabItem3 !=null && MyTabItem3.IsSelected)
    // do your staff
}

As you see the difference is to add checking for NULL.

That is it!

千仐 2024-12-23 17:49:13

我没有得到所选的工作答案,也许有些东西已经改变,也许我的设置不同。

我的解决方案很简单,您将发送者转变为 tabControle。然后,拉出选定的 TabItem (selectedValue) 并将其转换为 TabItem。

在我的情况下,我需要知道“谁”发生了变化,因此我查找 TabItem 的名称,以便更好地对特定事件做出反应。

XAML

<TabControl SelectionChanged="OnTabItemChanged">
    <TabItem Name="MainTap" Header="Dashboard"></TabItem
</TabControl>

C#

private async void OnTabItemChanged(object sender, SelectionChangedEventArgs e)
{

    TabControl tabControl = sender as TabControl; // e.Source could have been used instead of sender as well
    TabItem item = tabControl.SelectedValue as TabItem;
    if (item.Name == "MainTap")
    {
        Debug.WriteLine(item.Name);
    }
}

I didn't get the selected answer to work, maybe something has changed, maybe my setup is different.

My solutions is straightforward, you cast the sender to become the tabControle. Then you pull out the selected TabItem (selectedValue) and cast this to an TabItem.

In my situation, I need to know "who" changed, so I look for the name of the TabItem, to better react to a specific event.

XAML

<TabControl SelectionChanged="OnTabItemChanged">
    <TabItem Name="MainTap" Header="Dashboard"></TabItem
</TabControl>

C#

private async void OnTabItemChanged(object sender, SelectionChangedEventArgs e)
{

    TabControl tabControl = sender as TabControl; // e.Source could have been used instead of sender as well
    TabItem item = tabControl.SelectedValue as TabItem;
    if (item.Name == "MainTap")
    {
        Debug.WriteLine(item.Name);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文