标准WPF选项卡控件中是否有选定选项卡更改事件?

在WPF中,是否有一个事件可以用来确定TabControl的选定标签更改的时间?

我已经尝试过使用TabControl.SelectionChanged但是当一个标签中的一个孩子的select被改变时,它会被触发很多次。

我把它绑在处理程序中,使其工作:

 void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.Source is TabControl) { //do work when tab is changed } } 

如果将x:Name属性设置为每个TabItem如下所示:

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

然后你可以访问事件中的每个TabItem

 private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (MyTabItem1.IsSelected) // do your stuff if (MyTabItem2.IsSelected) // do your stuff if (MyTabItem3.IsSelected) // do your stuff } 

如果您只想在选中选项卡时发生事件,则这是正确的方法:

 <TabControl> <TabItem Selector.Selected="OnTabSelected" /> <TabItem Selector.Selected="OnTabSelected" /> <TabItem Selector.Selected="OnTabSelected" /> <!-- You can also catch the unselected event --> <TabItem Selector.Unselected="OnTabUnSelected" /> </TabControl> 

并在你的代码

  private void OnTabSelected(object sender, RoutedEventArgs e) { var tab = sender as TabItem; if (tab != null) { // this tab is selected! } } 

你仍然可以使用该事件。 只要检查发件人参数是你真正关心的控件,如果是的话,运行事件代码。

生成的事件正在冒泡,直到它被处理。

当在ListViewselect的项目发生更改时,下面的这个xaml部分触发ui_Tab_Changed后的ui_A_Changed ,而不pipeTabControlTabItem更改。

 <TabControl SelectionChanged="ui_Tab_Changed"> <TabItem> <ListView SelectionChanged="ui_A_Changed" /> </TabItem> <TabItem> <ListView SelectionChanged="ui_B_Changed" /> </TabItem> </TabControl> 

我们需要使用ui_A_Changed (和ui_B_Changed等)中的事件:

 private void ui_A_Changed(object sender, SelectionChangedEventArgs e) { // do what you need to do ... // then consume the event e.Handled = true; } 

此代码似乎工作:

  private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { TabItem selectedTab = e.AddedItems[0] as TabItem; // Gets selected tab if (selectedTab.Name == "Tab1") { // Do work Tab1 } else if (selectedTab.Name == "Tab2") { // Do work Tab2 } } 

这是正确的事件。 也许它没有正确连接?

 <TabControl SelectionChanged="TabControl_SelectionChanged"> <TabItem Header="One"/> <TabItem Header="2"/> <TabItem Header="Three"/> </TabControl> 

在代码隐藏….

 private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { int i = 34; } 

如果我在i = 34行上设置了一个断点,它只会在我更改标签时中断,即使标签有子元素并且其中一个被选中。

如果任何人使用WPF现代用户界面,他们不能使用OnTabSelected事件,但他们可以使用SelectedSourceChanged事件。

喜欢这个

 <mui:ModernTab Layout="Tab" SelectedSourceChanged="ModernTab_SelectedSourceChanged" Background="Blue" AllowDrop="True" Name="tabcontroller" > 

C#代码是

 private void ModernTab_SelectedSourceChanged(object sender, SourceEventArgs e) { var links = ((ModernTab)sender).Links; var link = this.tabcontroller.Links.FirstOrDefault(l => l.Source == e.Source); if (link != null) { var index = this.tabcontroller.Links.IndexOf(link); MessageBox.Show(index.ToString()); } } 

如果您使用的是MVVM模式,那么使用事件处理程序是不方便的(并且打破模式)。 相反,您可以将每个单独的TabItem的Selector.IsSelected属性绑定到viewmodel中的依赖项属性,然后处理PropertyChanged事件处理程序。 这样,您就可以确切地知道基于PropertyNameselect/取消select哪个选项卡,并且每个选项卡都有一个特殊的处理程序。

例如: MainView.xaml

 <TabControl> <TabItem Header="My tab 1" Selector.IsSelected="{Binding IsMyTab1Selected}"> ... </TabItem> <TabItem Header="My tab 2" Selector.IsSelected="{Binding IsMyTab2Selected}"> ... </TabItem> </TabControl> 

例如: MainViewModel.cs

 public bool IsMyTab1Selected { get { return (bool)GetValue(IsMyTab1SelectedProperty); } set { SetValue(IsMyTab1SelectedProperty, value); } } public static readonly DependencyProperty IsMyTab1SelectedProperty = DependencyProperty.Register("IsMyTab1Selected", typeof(bool), typeof(MainViewModel), new PropertyMetadata(true, new PropertyChangedCallback(MyPropertyChanged))); public bool IsMyTab2Selected { get { return (bool)GetValue(IsMyTab2SelectedProperty); } set { SetValue(IsMyTab2SelectedProperty, value); } } public static readonly DependencyProperty IsMyTab2SelectedProperty = DependencyProperty.Register("IsMyTab2Selected", typeof(bool), typeof(MainViewModel), new PropertyMetadata(false, new PropertyChangedCallback(MyPropertyChanged))); private void MyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.Property.Name == "IsMyTab1Selected") { // stuff to do } else if (e.Property.Name == "IsMyTab2Selected") { // stuff to do } } 

如果你的MainViewModelINotifyPropertyChanged而不是DependencyObject ,那就用这个代替:

例如: MainViewModel.cs

 public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public MainViewModel() { PropertyChanged += handlePropertyChanged; } public bool IsMyTab1Selected { get { return _IsMyTab1Selected ; } set { if (value != _IsMyTab1Selected ) { _IsMyTab1Selected = value; OnPropertyChanged("IsMyTab1Selected "); } } } private bool _IsMyTab1Selected = false; public bool IsMyTab2Selected { get { return _IsMyTab2Selected ; } set { if (value != _IsMyTab2Selected ) { _IsMyTab2Selected = value; OnPropertyChanged("IsMyTab2Selected "); } } } private bool _IsMyTab2Selected = false; private void handlePropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "IsMyTab1Selected") { // stuff to do } else if (e.PropertyName == "IsMyTab2Selected") { // stuff to do } }