2012-01-24 8 views
0

ユーザーコントロールをページとして渡そうとしています。私はボタンで作業しています。クリックイベントを含むメニューを追加すると、それはもはや機能しません。メニュー項目とボタンイベントに関する問題

これは、メインレイアウトにどのUserControlを入力するかを示すコードブロックです。この部分はボタンを使用するだけで動作します。

private void btnGeneral_Click(object sender, RoutedEventArgs e) 
    { 
     PanelMainContent.Children.Clear(); 
     Button button = (Button)e.OriginalSource; 
     PanelMainWrapper.Header = button.Content; 
     Type type = this.GetType(); 
     Assembly assembly = type.Assembly; 
     PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]); 
    } 

この部分はのMenuItemとボタンを使用しようと、それは

public void btnGeneral_Click(object sender, RoutedEventArgs e) 
    { 

     PanelMainContent.Children.Clear(); 
     MenuItem menuItem = (MenuItem)e.OriginalSource; 
     Button button = (Button)e.OriginalSource; 

     if (e.OriginalSource == menuItem) 
     { 
      PanelMainWrapper.Header = menuItem.Header; 
      Type type = this.GetType(); 
      Assembly assembly = type.Assembly; 
      PanelMainContent.Children.Add(_userControls[menuItem.Tag.ToString()]); 
     } 

     if (e.OriginalSource == button) 
     { 
      PanelMainWrapper.Header = button.Content; 
      Type type = this.GetType(); 
      Assembly assembly = type.Assembly; 
      PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]); 
     } 
    } 

エラー(S)Iレシーブを動作しません。

XamlParseException: 
The invocation of the constructor on type 'Test.MainWindow' that matches the specified binding constraints threw an exception.' Line number '5' and line position '9' 

InnerException 
{"Unable to cast object of type 'System.Windows.Controls.Button' to type 'System.Windows.Controls.MenuItem'."} 

いずれかのガイダンスをいただければ幸いです。

ありがとうございます!

答えて

2

...

if (e.OriginalSource == menuItem) 

...あなたの代わりに、このようにそれを確認することができます。

if(e.OriginalSource is MenuItem) 

次に、あなたのifブロック内で、あなたの変数宣言を移動することができます。最終的なコードは次のようになります:

public void btnGeneral_Click(object sender, RoutedEventArgs e) 
{ 
    PanelMainContent.Children.Clear(); 

    if (e.OriginalSource is MenuItem) 
    { 
     MenuItem menuItem = (MenuItem)e.OriginalSource; 
     PanelMainWrapper.Header = menuItem.Header; 
     Type type = this.GetType(); 
     Assembly assembly = type.Assembly; 
     PanelMainContent.Children.Add(_userControls[menuItem.Tag.ToString()]); 
    } 

    if (e.OriginalSource is Button) 
    { 
     Button button = (Button)e.OriginalSource; 
     PanelMainWrapper.Header = button.Content; 
     Type type = this.GetType(); 
     Assembly assembly = type.Assembly; 
     PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]); 
    } 
} 
3

あなたがここにMenuItemとしてButtonをキャストしようとしている:

MenuItem menuItem = (MenuItem)e.OriginalSource; 
Button button = (Button)e.OriginalSource; 

私はそれのために正確な用語を忘れて、しかし、この方法で代わりにそれをキャスト:このメソッドが返す

MenuItem menuItem = e.OriginalSource as MenuItem; 
Button button = e.OriginalSource as Button; 

nullキャストされるオブジェクトが予想される型でない場合、例外をスローしません。あなたのmenuItembutton変数が使用しようとする前にnullでないことを確認してください。代わりに、このようなソースの種類をチェックする

関連する問題