2015-05-16 24 views
1

我對WPF非常陌生,目前正在開發一個項目來測試我的知識。檢查WPF面板中特定的Child ViewModel

我有,我想改變不同意見的小組,但只有當它是不是面板的當前子元素..認爲應該改變所以我寫的代碼看起來像這樣

public void OnAddNewQuestionBank(object paramter) 
{ 
     var childIsCurrent = _mainWindow.ContentBox.Children.OfType<AddQuestionsPane>().Equals(typeof(AddQuestionsPane)); 

     if(!childIsCurrent) 
     { 
      //Display the panel 
      _mainWindow.ContentBox.Children.Add(new AddQuestionsPane()); 
     }    
} 

但我意識到,childIsCurrent總是返回false ...我該如何解決這個問題

回答

2

OfType<T>()返回IEnumerable<T>對象。它不等於Type,這就是爲什麼總是返回false。相反,您可以嘗試查看IEnumerable<T>中是否有任何物品。例如:

var childIsCurrent = _mainWindow.ContentBox.Children.OfType<AddQuestionsPane>().Any(); 
+0

哦,我明白了......謝謝你, –