2015-04-29 39 views
0

我在WPF中使用TabControl的Tab_SelectionChanged事件。它包含3個選項卡項目。我必須限制用戶導航到其他標籤,即設置和時間表,而主頁選項卡上的工作正在進行中。雖然使用事件我面臨一個問題,即如果我點擊設置選項卡它會顯示一個彈出「你不能導航,而工作正在進行中」,當我點擊設置選項卡上點擊時間表選項卡時,它顯示我兩次相同的彈出。這背後的原因是設置選項卡仍然selected.Here是我此代碼:Tab項目保持選中WPF

private void tabMHPC_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      TabControl tab = (TabControl)sender; 
      if (tab.SelectedIndex != -1) 
      { 
       if (tab.SelectedIndex != 4 && tab.SelectedIndex != 1 && tab.SelectedIndex != 0) 
       { 
        if (scanStatus == "fixing") 
        { 
         MessageBox.Show(ApplicationInfo.ApplicationName + " is still busy in fixing issues.Please let the fixation complete.", ApplicationInfo.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information); 
         homeTab.IsSelected = true; 
        } 
        else 
        { 
         MessageBox.Show(ApplicationInfo.ApplicationName + " is still busy scanning issues.Please stop it before you leave the Home tab.", ApplicationInfo.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information); 
         homeTab.IsSelected = true; 
        } 
       } 

       else if (tab.SelectedIndex == 0) 
       { 

       } 
      } 
     } 

我想以前的標籤項目IsSelected屬性獲取假,當我在其他TabItem的移動。的

回答

0

而是處理SelectionChanged事件,你應該數據的適當類型的屬性綁定到TabControl.SelectedItem property

<TabControl SelectedItem="{Binding YourSelectedItemProperty}" ... /> 

當你做到這一點,您將能夠停止TabItem被改變:

public YourDataType YourSelectedItemProperty 
{ 
    get { return yourSelectedItemProperty; } 
    set 
    { 
     if (isOkToChangeTabItem) 
     { 
      yourSelectedItemProperty = value; 
      NotifyPropertyChanged("YourSelectedItemProperty"); 
     } 
    } 
} 

溶液的最後一部分是將isOkToChangeTabItem變量設置爲truefalse取決於它是否是行用於用戶改變是否選擇TabItem

+0

感謝您的回答。但我希望用戶單擊其他選項卡,並且當用戶從一個選項卡移動到另一個選項時,比先前的IsSelected屬性更改爲false。 – Nonit

+0

我不完全確定你爲什麼要這樣做,但是你使用了錯誤的方法。您不應該在後面的代碼中持有對單個「TabItem」的引用。你應該是數據綁定。 – Sheridan