I have a TabControl in WPF. I want to find an event that occurs when changing tabs. What is the name of this event?
我在WPF中有一个TabControl。我想要找到一个在切换选项卡时发生的事件。这个事件的名字是什么?
3 个解决方案
#1
20
The TabControl
inherits from a Selector
which contains the SelectionChanged
event.
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 ...
}
}
#2
2
I just want to add my point here. And I will use cool answer of @pratap k to do it.
我想在这里加上我的观点。我将使用@pratap k的cool answer来完成。
<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
.
正如您看到的,区别在于添加了NULL检查。
That is it!
这就是它!
#3
2
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.
我的解决方案很简单,您将发送方转换为tabControle。然后,取出所选的TabItem (selectedValue)并将其转换为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.
在我的情况下,我需要知道“谁”更改了,所以我要查找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);
}
}
#1
20
The TabControl
inherits from a Selector
which contains the SelectionChanged
event.
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 ...
}
}
#2
2
I just want to add my point here. And I will use cool answer of @pratap k to do it.
我想在这里加上我的观点。我将使用@pratap k的cool answer来完成。
<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
.
正如您看到的,区别在于添加了NULL检查。
That is it!
这就是它!
#3
2
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.
我的解决方案很简单,您将发送方转换为tabControle。然后,取出所选的TabItem (selectedValue)并将其转换为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.
在我的情况下,我需要知道“谁”更改了,所以我要查找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);
}
}