locked
MenuItem ItemsSource RRS feed

  • Question

  • Hello,

    I would like to create a context menu for my listview:

    That would have the following structure:

    Send to Admin
    Send to >
             John
             Jim
             Jill
             Jack

    The list of names is dynamic is encapsulated in a string array and accessible through the TEST property. 

    <ListView.ContextMenu>
         <ContextMenu DataContext="{Binding ElementName=pg}">
          <MenuItem Header="Send to Admin"/>
          <MenuItem
                Header="Send to"
                ItemsSource="{Binding Path=TEST}">
          </MenuItem>
         </ContextMenu>
     </ListView.ContextMenu>

    So I can't tell why this doesn't work :(

    I tried ItemsSource={Binding ElementName=pg, Path=TEST}...no cigar.

    does the itemsSource have to be a related to a HierarchicalDataTemplate

    Thanks

    Houman

    Tuesday, September 26, 2006 10:53 PM

Answers

  • If you set the ItemsSource directly in code it is working, if you bind it is not working, may be a bug!
    Wednesday, September 27, 2006 9:33 AM
  • I had a similar problem, and unfortunately didn't manage to find an all-XAML solution.

    The problem is that ContextMenu is simply a property of ListView, not one of its children. That means that ContextMenu isn't part of ListView's logical or physical tree. As a consequence of this, the {Binding ElementName=pg} markup extension on DataContext has no tree up which to climb to search for the element named pg, and so DataContext doesn't get set. You can read more about towards the end of this post, and in the comments: http://blogs.msdn.com/nickkramer/archive/2006/08/18/705116.aspx

    In the end, I had to solve the problem by using codebehind to set the DataContext of the ContextMenu.

    Wednesday, September 27, 2006 9:36 AM

All replies

  • If you set the ItemsSource directly in code it is working, if you bind it is not working, may be a bug!
    Wednesday, September 27, 2006 9:33 AM
  • I had a similar problem, and unfortunately didn't manage to find an all-XAML solution.

    The problem is that ContextMenu is simply a property of ListView, not one of its children. That means that ContextMenu isn't part of ListView's logical or physical tree. As a consequence of this, the {Binding ElementName=pg} markup extension on DataContext has no tree up which to climb to search for the element named pg, and so DataContext doesn't get set. You can read more about towards the end of this post, and in the comments: http://blogs.msdn.com/nickkramer/archive/2006/08/18/705116.aspx

    In the end, I had to solve the problem by using codebehind to set the DataContext of the ContextMenu.

    Wednesday, September 27, 2006 9:36 AM
  • But, we should be able to bind  the ItemsSource Property of the MenuItem
    Wednesday, September 27, 2006 10:00 AM
  • Thank you Lee & Sam,

    I set the itemssource in code and as you had promised it works!

    Regards,

    Houman

    Wednesday, September 27, 2006 1:01 PM
  • There are more interesting things happened here:

    <Button Name="b1" Content="clickme">
                <Button.ContextMenu>
                        <ContextMenu>
                    <MenuItem Header="SendToAdmin" Name="sendMenu"/>
               
                <!--This binding statement doesn't work-->
                    <MenuItem
                                  Header="SendTo"
                                  ItemsSource="{Binding Path=Header, ElementName=sendMenu}"/>
                <!--This binding statement works-->
                            <MenuItem
                                  Header="SendTo"
                                  ItemsSource="{Binding Path=Header, RelativeSource={RelativeSource Self}}"/>
                    </ContextMenu>
                </Button.ContextMenu>
            </Button>

        For the binding statement which doesn't work, I get this error from the output window:
    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=sendMenu'. BindingExpression:Path=Header; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

        I think sendMenu is part of ContenxtMenu's logical tree, so it should know what "sendMenu" is.
        Can someone from microsoft explain what's going on here?

    Sheva
    Wednesday, September 27, 2006 3:32 PM