Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to add a Context Menu and bind a command to that in my tree as shown in code below. I get it, Since I am adding the Context Menu in ListBox then I will get the DataContext of List box.
So here my question is How can I get the DataContext of ItemsControl while creating the Context Menu at ListBox.

Please let me know your suggestions.
Thanks.

XML
<ItemsControl ItemsSource="{Binding Path=CollegeViewModel}">
  <ItemsControl.ItemTemplate>

     <HierarchicalDataTemplate>
       <StackPanel>
         <Label Content="{Binding Path=DisplayText}"/>
           <TreeView ItemsSource="{Binding Path=Children}">
              <TreeView.Resources>

               <HierarchicalDataTemplate DataType="{x:Type common:StudentItem}" ItemsSource="{Binding Path=Children}">
               <TextBlock Text="{Binding Path=DisplayText}"/>
               </HierarchicalDataTemplate>

               <HierarchicalDataTemplate DataType="{x:Type common:TeacherItem}" ItemsSource="{Binding Path=Children}">
               <TextBlock Text="{Binding Path=DisplayText}"/>
               </HierarchicalDataTemplate>


              <HierarchicalDataTemplate DataType="{x:Type common:LecturerItem}" ItemsSource="{Binding Path=Children}">
              <TextBlock Text="{Binding Path=DisplayText}" />
              </HierarchicalDataTemplate>

                 <DataTemplate DataType="{x:Type common:Leaf}">

                  <ListBox ItemsSource="{Binding Children}" >                                          
                    <ListBox.ContextMenu>
                      <ContextMenu>
                       <MenuItem Header="QuickView" Command="{Binding ExitCommand}"/>
                      </ContextMenu>
                     </ListBox.ContextMenu>                                         
                   </ListBox>

                 </DataTemplate>
                                 
                                   
               </TreeView.Resources>

              </TreeView>
            </StackPanel>
         </HierarchicalDataTemplate>

       </ItemsControl.ItemTemplate>
    </ItemsControl>
Posted
Comments
U. G. Leander 23-Nov-15 4:29am    
The following link might help you...I didn't want to post this as an answer as I found it on the internet myself ;-)

http://stackoverflow.com/questions/1511516/bind-to-itemscontrols-datacontext-from-inside-an-itemtemplate

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