Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Working with accordion, i create Accordion Items on the .cs page,
I need Catch the selected accordionItem?
C#
AccordionItem RSR = new AccordionItem();
                                                   RSR.Header = "RSR";

                                                         AccordionItem Supervisors = new AccordionItem();
                                                                     Supervisors.Header = "Supervisors";
                                                                AccordionItem Managers = new AccordionItem();
                                                                             Managers.Header = "Managers";

                                                                           // Managers.
                                                                             
                                                                             ContextMenu RSRContext=new ContextMenu();
                                                                             ContextMenu SupervisorsContext=new ContextMenu();
                                                                             ContextMenu ManagersContext=new ContextMenu();
                                                                             

                                                                             foreach (CrmUser user in RAU.users)
                                                                             {

                                                                                 if (user.jobtitle == 0)
                                                                                  RSRContext.Items.Add(user);
                                                                                
                                                                                 if (user.jobtitle == 4)
                                                                                     SupervisorsContext.Items.Add(user);
                                                                                 
                                                                                 if (user.jobtitle == 5)
                                                                                     ManagersContext.Items.Add(user);
                                                                                 

                                                                             }

                                                                             RSR.Content = RSRContext;
                                                                             Supervisors.Content = SupervisorsContext;
                                                                             Managers.Content = ManagersContext;

                                                                            

                                                                             accordion1.Items.Add(RSR);
                                                                             accordion1.Items.Add(Supervisors);
                                                                             accordion1.Items.Add(Managers);
                                                                            
                                                                             accordion1.SelectionMode = AccordionSelectionMode.ZeroOrOne;
Posted
Updated 29-Jun-12 0:34am
v3

1 solution

Just to comment first, please make sure any source code is neatly formatted when you ask a question. Unless people can read it they probably won't help you. It's an absolute nightmare trying to read that, but as your question/problem is a good one I'll answer anyway...

Here's a working example based on your code. The key bits are in bold...

void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            AccordionItem RSR = new AccordionItem();
            RSR.Header = "RSR";
            AccordionItem Supervisors = new AccordionItem();
            Supervisors.Header = "Supervisors";
            AccordionItem Managers = new AccordionItem();
            Managers.Header = "Managers";

            ContextMenu RSRContext = new ContextMenu();
            ContextMenu SupervisorsContext = new ContextMenu();
            ContextMenu ManagersContext = new ContextMenu();            

            // I've change this bit as you haven't explained "RAU.users"
            // The important thing is I've used a MenuItem 
            // (You'll need to convert your "user" objects to "MenuItems")
            // And also attached Click listeners
            MenuItem jeff = new MenuItem { Header = "Jeff" };
            jeff.Click += new RoutedEventHandler(MenuSelected_Click);
            MenuItem bob = new MenuItem { Header = "Bob" };
            bob.Click += new RoutedEventHandler(MenuSelected_Click);
            RSRContext.Items.Add(jeff);
            RSRContext.Items.Add(bob);
            MenuItem sheenagh = new MenuItem { Header = "Sheenagh" };
            sheenagh.Click += new RoutedEventHandler(MenuSelected_Click);
            MenuItem mike = new MenuItem { Header = "Mike" };
            mike.Click += new RoutedEventHandler(MenuSelected_Click);
            SupervisorsContext.Items.Add(sheenagh);
            SupervisorsContext.Items.Add(mike);
            MenuItem alice = new MenuItem { Header = "Alice" };
            alice.Click += new RoutedEventHandler(MenuSelected_Click);
            MenuItem luke = new MenuItem { Header = "Luke" };
            luke.Click += new RoutedEventHandler(MenuSelected_Click);
            ManagersContext.Items.Add(alice);
            ManagersContext.Items.Add(luke);
            // End of change

            RSR.Content = RSRContext;
            Supervisors.Content = SupervisorsContext;
            Managers.Content = ManagersContext;
            
            accordion1.Items.Add(RSR);
            accordion1.Items.Add(Supervisors);
            accordion1.Items.Add(Managers);

            accordion1.SelectionMode = AccordionSelectionMode.ZeroOrOne;            
        }

        void MenuSelected_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = sender as MenuItem;
            MessageBox.Show("You clicked on: " + item.Header.ToString());
        }


PS Please mark as the answer if this helps.
 
Share this answer
 
v2
Comments
Adam David Hill 1-Jul-12 7:26am    
Just as an additional remark - the key is to create child items which have click events - it's not a problem with the Accordion, rather with how you're handling the items which you put in. As you were using ContextMenus, then to interact with their own children, use MenuItems. Hopefully that's clear from the code above.
Adam David Hill 2-Jul-12 4:39am    
That's a separate question, so you might want to post it separately to this thread. I don't have VS handy to try but I suspect you need to use control templates. If that doesn't work consider rethinking the use of the context menu in favour of using your own control. I'd need more information about what you want to do to advise what the most appropriate controls would be. You could probably use templated ListBoxes instead of ContextMenus, for example.
Adam David Hill 2-Jul-12 4:54am    
Could the down-voter please explain the negative mark? The code above is tested and working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900