+ Reply to Thread
Results 1 to 2 of 2

Thread: issue with click events

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10

    issue with click events

    I'm trying to pass user controls as pages. I have it working with buttons. When I added a menu with click events it no longer works , i use that code :
    Code:
    private void bGeneral_Click(object sender, RoutedEventArgs e)
        {
            PanelMainContent.Children.Clear();
            Button b1 = (Button)e.OriginalSource;
            PanelMainWrapper.Header = b1.Content;
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            PanelMainContent.Children.Add(_userControls[b1.Tag.ToString()]);
        }
    This Part attempts to use the MenuItems and Buttons, It does not work
    Code:
    public void bGeneral_Click(object sender, RoutedEventArgs e)
        {   
    
            PanelMainContent.Children.Clear();
            MenuItem menuItem = (MenuItem)e.OriginalSource;
            Button b1 = (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 == b1)
            {
                PanelMainWrapper.Header = button.Content;
                Type type = this.GetType();
                Assembly assembly = type.Assembly;
                PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
            }
        }

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    Instead of using :
    Code:
    if (e.OriginalSource == menuItem)
    Use :
    Code:
    if(e.OriginalSource is menuItem)
    Then you can move your variable declarations inside your if blocks. So your final code looks like this:
    Code:
    public void bGeneral_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 b1 = (Button)e.OriginalSource;
            PanelMainWrapper.Header = b1.Content;
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
        }
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts