2012-06-13 202 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

相關問題