locked
Nested button contextmenu placement problem RRS feed

  • Question

  • I have the following button:

    <Button Name="btnOpen" Click="btnOpen_Click"
            ToolTip="Browse resource folder"
            ContextMenuOpening="btnOpen_ContextMenuOpening"
            ContextMenuService.Placement="Bottom">
        <StackPanel Orientation="Horizontal">
            <Image Source="pack://application:,,,/images/OpenFile.png"/>
            <Label>Br_owse</Label>
            <Button Name="btnMRU"
                    BorderBrush="{x:Null}"
                    Click="btnMRU_Click">
                <Image Source="pack://application:,,,/images/Dropdown.png"/>
            </Button>
        </StackPanel>
        <Button.ContextMenu>
            <ContextMenu>
                <MenuItem Header="test"/>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>

    The button serves as a normal "open" button with a recent file list dropdown. If I right click on the button, its contextmenu is shown correctly below the button. However that's not what I want. I need to show the contextmenu when the inner dropdown button is clicked:

    private void btnMRU_Click(object sender, RoutedEventArgs e)
    {
        e.Handled = true;
        btnOpen.ContextMenu.IsOpen = true;
    }
    But it seems WPF runtime has some inner working so that the contextmenu is displayed at clicked cursor position. How to avoid this problem?


    Ignorance is blissful


    • Edited by Neo the 1 Monday, September 9, 2019 3:11 PM
    Monday, September 9, 2019 1:52 PM

Answers

  • Hi,

    make some settings for the ContextMenu

      private void BtnMRU_Click(object sender, RoutedEventArgs e)
            {
                //target
                btnOpen.ContextMenu.PlacementTarget = this.btnOpen;
                //location
                btnOpen.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom ;   
                //show 
                btnOpen.ContextMenu.IsOpen = true;
            }

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Neo the 1 Tuesday, September 10, 2019 11:04 AM
    Tuesday, September 10, 2019 7:21 AM

All replies

  • Hi,

    make some settings for the ContextMenu

      private void BtnMRU_Click(object sender, RoutedEventArgs e)
            {
                //target
                btnOpen.ContextMenu.PlacementTarget = this.btnOpen;
                //location
                btnOpen.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom ;   
                //show 
                btnOpen.ContextMenu.IsOpen = true;
            }

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Neo the 1 Tuesday, September 10, 2019 11:04 AM
    Tuesday, September 10, 2019 7:21 AM
  • Ah, it works by setting PlacementTarget in code. I had tried setting it in XAML (PlacementTarget={Binding ElementName=btnOpen}"), but it did not work.

    Ignorance is blissful

    Tuesday, September 10, 2019 11:09 AM