2012-06-13 131 views
3

我正在WPF上工作,我正在创建一个包含TabControl的userControl,它有一些TabItems。TabControl的SelectionChanged事件问题

我需要执行一些东西,当选定的选项卡更改,所以,我试图做的是使用事件myTabControl.SelectionChanged,但它被提出很多次,即使我只点击一次TabItem。然后我看到这篇文章is-there-selected-tab-changed-event-in-the-standard-wpf-tab-control,并把这个代码我的方法中:

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

这样做的第一个问题已经解决后,但后来当我跑的应用程序,并试图改变标签的,有人提出一个错误:

Dispatcher processing has been suspended, but messages are still being processed 

的Visual Studio点的代码if (e.Source is TabControl) { //here }

里面的第一线,但我发现这篇文章selectionchanged-event-firing-exceptions-for-unknown-reasons,我可以解决这个问题下面写一些代码:

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

但现在我有一个问题,我还没有能够解决:

该事件被解雇的两倍!另一个奇怪的是,只有我第一次尝试改变选定的选项卡的事件引发两次,但选定的选项卡仍然是相同的

我希望有人可以帮助我,提前谢谢。

回答

4

我想我需要休息一下,因为我的问题真的很傻。

原来那不是TabControl我应该用TabItem,因为它是我在有趣的对照

所以,我的代码必须如下:

void mainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (e.Source is TabItem) 
     {  
      if (this.IsLoaded) 
      { 
       //do work when tab is changed 
      } 
     } 
    } 
+1

我不得不使用TabControl,但帮助感谢 – Brent

+0

所以我布伦特,保存my_Ass,谢谢 – TripleAntigen