Disconnect. Right click and enable left click for context menu in WPF using MVVM.

the code:

<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>

I am using the MVVM pattern. In ViewModel, I have an IsMenuOpen property that controls the context menu, open close. The problem is that I can turn off the right click and not show the context menu on the left click.

+5
source share
5 answers

You can also check the attached property ContextMenuService.IsEnabledon the parent control. It will block only a right-click, and you can still display the menu manually when you click the left mouse button, so in accordance with the previous example:

<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;
}
+18
source

XAML, .

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;
}
+2

:

  • , DataContext ContextMenu .
  • , IsOpen TwoWay.
  • , , PlacementTarget , Button.ContextMenu.PlacementTarget = this, IsMenuOpen = true, .

:

<Style x:Key="SubjectButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource CommandButtonStyle}">
<Setter Property="Foreground" Value="Green" />
<Setter Property="ContextMenu">
    <Setter.Value>
        <ContextMenu DataContext="{Binding PlacementTarget.DataContext.Manager, RelativeSource={RelativeSource Self}}" 
                     ItemsSource="{Binding SubjectManager.ContextMenuItems}"
                     IsOpen="{Binding SubjectManager.ContextMenuIsOpen, Mode=TwoWay}">
            <ContextMenu.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{Binding OnClick}" />
                </Style>
            </ContextMenu.ItemContainerStyle>
        </ContextMenu>
    </Setter.Value>
</Setter>
<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Foreground" Value="DarkGreen" />
    </Trigger>
</Style.Triggers>
</Style>

:

public void ShowContextMenu(SearchCondition searchCondition, Button button)
{
    button.ContextMenu.DataContext = this;
    SubjectManager.OpenContextMenu();
}
+2

, Popup. , ...

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

:

<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;
}
0

All Articles