使用MVVM在WPF中禁用右键单击并启用左键单击上下文菜单

使用MVVM在WPF中禁用右键单击并启用左键单击上下文菜单,wpf,contextmenu,Wpf,Contextmenu,代码: 我使用的是MVVM模式。在VIEWMIDE中,我有一个属性“ISMunuOutlook”,它控制上下文菜单打开关闭。问题是,我能够禁用右击,并且不能在左键上显示上下文菜单。 < P>如果要将菜单绑定到属性,请考虑弹出式控件。它具有与上下文菜单类似的功能,但不绑定到特定的鼠标按钮 <Button Style="{StaticResource HPForegroundStyle}" IsTabStop="False" Command

代码:



我使用的是MVVM模式。在VIEWMIDE中,我有一个属性“ISMunuOutlook”,它控制上下文菜单打开关闭。问题是,我能够禁用右击,并且不能在左键上显示上下文菜单。

< P>如果要将菜单绑定到属性,请考虑弹出式控件。它具有与上下文菜单类似的功能,但不绑定到特定的鼠标按钮

<Button Style="{StaticResource HPForegroundStyle}" IsTabStop="False"                 
        Command="{Binding ForegroundPhoneCommand}"  Click="Button_Click">
                    <Button.ContextMenu>                   
                        <ContextMenu ItemsSource="{Binding OptionsMenuItemList}"                            ItemContainerStyle="{StaticResource ContextMenuItemStyle}" 
                                     IsOpen="{Binding IsMenuOpen}"                                        
                                     PlacementTarget="{Binding RelativeSourc={RelativeSource AncestorType={x:Type Button}}}">
                        </ContextMenu>
                    </Button.ContextMenu>
    </Button>

使用类似于问题的XAML,这对我很有效

<Popup IsVisible = {Binding IsMenuOpen} >
    <!-- Fill in what you need here -->
</Popup>

你可以这样做:

private bool _isMenuOpen = false;
public bool IsMenuOpen 
{
    get { return _isMenuOpen; }
    set 
    {
        // Don't allow the UI (right-click) to set this property to true
        if (!value)
            _isMenuOpen = value;
    }
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    Button btn = sender as Button;
    _isMenuOpen = true;
    btn.ContextMenu.IsOpen = true;
}

私有无效btn_单击(对象发送方,路由目标)
{
popup.Visibility=可见性.Visibility;
popup.IsOpen=true;
}
私有void btn_MouseRightButtonDown(对象发送器,MouseButtonEventArgs e)
{
popup.Visibility=Visibility.Collapsed;
}

需要注意的几件事:

  • 确保ContextMenu的DataContext有效
  • 确保IsOpen是双向绑定
  • 如果要尝试左键单击打开,请记住PlacementTarget无效,因此必须设置Button.ContextMenu.PlacementTarget=this,然后IsMenuOpen=true才能显示它
  • 我的代码段供参考:

    <Button x:Name="btn" Click="btn_Click" MouseRightButtonDown="btn_MouseRightButtonDown">
        <Button.ContextMenu>
            <ContextMenu x:Name="popup" Visibility="Collapsed">
                <MenuItem Header="aaa"></MenuItem>
                <MenuItem Header="bbb"></MenuItem>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>
    
    private void btn_Click(object sender, RoutedEventArgs e)
    {
        popup.Visibility = Visibility.Visible;
        popup.IsOpen = true;
    }
    
    private void btn_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        popup.Visibility = Visibility.Collapsed;
    }
    

    您还可以检查父控件上的
    ContextMenuService.IsEnabled
    attached属性。它将仅阻止右键单击,并且您仍然能够在左键单击时手动显示菜单,因此根据前面的示例:

    public void ShowContextMenu(SearchCondition searchCondition, Button button)
    {
        button.ContextMenu.DataContext = this;
        SubjectManager.OpenContextMenu();
    }
    
    
    ...
    私有无效btn_单击(对象发送方,路由目标)
    {
    popup.Visibility=可见性.Visibility;
    popup.IsOpen=true;
    }
    
    如何打开关联菜单?它是一个弹出式菜单还是实际的上下文菜单控件,当你右键单击时,它不会打开,但左键单击也不会打开它?ForeGroundPhone命令是否导致IsMenuOpen属性设置为true?IsMenuOpen属性是否实现InotifyPropertyChange查看AttachedCommandBehavior。它可能会帮助您…ForeGroundPhone命令将IsMenuOpen属性设置为true并实现InotifyPropertyChange您是否检查了ContextMenu是否正确绑定?检查您的输出屏幕是否有错误,这显然根本不起作用。。我的上下文菜单仍然出现,并立即消失
    public void ShowContextMenu(SearchCondition searchCondition, Button button)
    {
        button.ContextMenu.DataContext = this;
        SubjectManager.OpenContextMenu();
    }
    
    <Button x:Name="btn" Click="btn_Click" ContextMenuService.IsEnabled="false">
        <Button.ContextMenu>
            <ContextMenu x:Name="popup">
             ...
            </ContextMenu>
        </Button.ContextMenu>
    </Button>
    
    private void btn_Click(object sender, RoutedEventArgs e)
    {
        popup.Visibility = Visibility.Visible;
        popup.IsOpen = true;
    }