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 MVVM pattern. In ViewModel i have a property 'IsMenuOpen' which controls context menu open close .Problem is that i'm able to disable right click and not able to show context menu on left click.
You can check also ContextMenuService.IsEnabled
attached property on the parent control. It will block only right click and you will still be able to show the menu manually on left click, so based on 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;
}
This worked for me using XAML similar to the question.
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;
}
A few things to keep track of:
My code snippet for reference:
<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>
And in the view model:
public void ShowContextMenu(SearchCondition searchCondition, Button button)
{
button.ContextMenu.DataContext = this;
SubjectManager.OpenContextMenu();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With