Cannot show ContextMenu programmatically when instantiating it from Resources
Posted: 14 Sep 2017, 10:24
Hi,
I am experimenting on how to show context menu using triggers other than Right-click.
The following code works well:
XAML
C++
Now I changed the XAML to use ContextMenu from a Resource
Now clicking the button doesn't show the context menu. What did I do wrong ?
I also observed that after clicking the button, the right-click event also ceased to work (i.e. context menu is not shown either with right-click).
Thanks.
I am experimenting on how to show context menu using triggers other than Right-click.
The following code works well:
XAML
Code: Select all
<UserControl x:Class="NoesisTutorial.ContextMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:noesis="clr-namespace:Noesis"
xmlns:local="clr-namespace:NoesisTutorial"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="AntiqueWhite">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Grid x:Name="MyGrid" Grid.Row="0" Background="WhiteSmoke"
ContextMenuService.Placement="Center"
ContextMenuService.IsEnabled="True"
ContextMenuService.PlacementTarget="{Binding ElementName=MyGrid}"
>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Test1"></MenuItem>
<MenuItem Header="Test2"></MenuItem>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
<Button x:Name="MyButton" Grid.Row="1" Click="Button_Click">Click to open ContextMenu</Button>
</Grid>
</UserControl>
Code: Select all
void ContextMenu::Button_Click(BaseComponent* sender, const RoutedEventArgs & e) {
auto grid = this->FindName<Grid>("MyGrid");
auto menu = grid->GetContextMenu();
menu->SetIsOpen(true);
}
Code: Select all
<UserControl x:Class="NoesisTutorial.ContextMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:noesis="clr-namespace:Noesis"
xmlns:local="clr-namespace:NoesisTutorial"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ContextMenu x:Key="myContextMenu">
<MenuItem Header="Test1"></MenuItem>
<MenuItem Header="Test2"></MenuItem>
</ContextMenu>
</UserControl.Resources>
<Grid Background="AntiqueWhite">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Grid x:Name="MyGrid" Grid.Row="0" Background="WhiteSmoke"
ContextMenuService.Placement="Center"
ContextMenuService.IsEnabled="True"
ContextMenuService.PlacementTarget="{Binding ElementName=MyGrid}"
ContextMenu="{StaticResource myContextMenu}"
>
</Grid>
<Button x:Name="MyButton" Grid.Row="1" Click="Button_Click">Click to open ContextMenu</Button>
</Grid>
</UserControl>
I also observed that after clicking the button, the right-click event also ceased to work (i.e. context menu is not shown either with right-click).
Thanks.