WPF Context menu on left click

后端 未结 8 485
情深已故
情深已故 2020-12-01 10:39

I have a WPF application..In which I have an Image control in Xaml file.

On right click of this image I have a context menu.

I would like to have same to be

相关标签:
8条回答
  • 2020-12-01 11:23

    XAML

        <Button x:Name="b" Content="button"  Click="b_Click" >
            <Button.ContextMenu >
                <ContextMenu   >
                    <MenuItem Header="Open" Command="{Binding OnOpen}" ></MenuItem>
                    <MenuItem Header="Close" Command="{Binding OnClose}"></MenuItem>                    
                </ContextMenu>
            </Button.ContextMenu>
        </Button>
    

    C#

        private void be_Click(object sender, RoutedEventArgs e)
            {
            b.ContextMenu.DataContext = b.DataContext;
            b.ContextMenu.IsOpen = true;            
            }
    
    0 讨论(0)
  • 2020-12-01 11:25

    you can bind the Isopen Property of the contextMenu to a property in your viewModel like "IsContextMenuOpen". but the problem is your can't bind directly the contextmenu to your viewModel because it's not a part of your userControl hiarchy.So to resolve this you should bing the tag property to the dataontext of your view.

    <Image Tag="{Binding DataContext, ElementName=YourUserControlName}">
    <ContextMenu IsOpen="{Binding PlacementTarget.Tag.IsContextMenuOpen,Mode=OneWay}" >
    .....
    </ContextMenu>
    <Image>
    

    Good luck.

    0 讨论(0)
提交回复
热议问题