nikobarli
Topic Author
Posts: 186
Joined: 26 Apr 2017, 06:23

Cannot show ContextMenu programmatically when instantiating it from Resources

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
<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>
C++
    void ContextMenu::Button_Click(BaseComponent* sender, const RoutedEventArgs & e) {
        auto grid = this->FindName<Grid>("MyGrid");
        auto menu = grid->GetContextMenu();
        menu->SetIsOpen(true);
    }
Now I changed the XAML to use ContextMenu from a Resource
<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>
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.
 
Wanderer
Posts: 168
Joined: 08 May 2017, 18:36

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

14 Sep 2017, 13:38

Try this, I can't test it now, (nspired from this: https://stackoverflow.com/questions/276 ... -reference )
simply change to ContextMenu="{Binding myContextMenu}" instead StaticResource (it is working for me, but I binding text instead context menu) or
<UserControl.Resources>
		<dataContextMenu x:Key="cm">
			<ContextMenu x:Key="myContextMenu">
				<MenuItem Header="Test1"></MenuItem>
				<MenuItem Header="Test2"></MenuItem>
			</ContextMenu>
		</dataContextMenu>
    </UserControl.Resources>
	// ..
	<Grid x:Name="MyGrid" Grid.Row="0" Background="WhiteSmoke" 
				DataContext="{StaticResource cm}"
              ContextMenuService.Placement="Center"
              ContextMenuService.IsEnabled="True"
              ContextMenuService.PlacementTarget="{Binding ElementName=MyGrid}"
              ContextMenu="{Binding myContextMenu}"
              >
        </Grid>
It is not working because Grid dont have dataContext for resource.
 
nikobarli
Topic Author
Posts: 186
Joined: 26 Apr 2017, 06:23

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

15 Sep 2017, 06:09

@Wanderer, thanks for the workaround. However, I need to know why using StaticResource doesn't work (while in WPF it is working). I am thinking to put ContextMenu in the resource, customize it using DataTemplate, then share it across the components.
 
nikobarli
Topic Author
Posts: 186
Joined: 26 Apr 2017, 06:23

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

15 Sep 2017, 09:55

Found another issue. When trying to bind IsOpen property of the context menu, the view freezes and nothing happens.

# The reason that I was trying to bind IsOpen is so that I can trigger the ContextMenu from VM code (without needing to directly access the View layer).

Slightly modify the XAML from the first code above reproduce it. It doesn't matter that "AnyProperty" exists or not.
<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 IsOpen="{Binding AnyProperty}">
                    <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>
 
User avatar
sfernandez
Site Admin
Posts: 3364
Joined: 22 Dec 2011, 19:20

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

19 Sep 2017, 12:41

I need to know why using StaticResource doesn't work (while in WPF it is working). I am thinking to put ContextMenu in the resource, customize it using DataTemplate, then share it across the components.
It is a bug in Noesis, because our code was assuming that ContextMenu was defined inside the ContextMenu property of the target element. Could you please report it in our bugtracker?
Found another issue. When trying to bind IsOpen property of the context menu, the view freezes and nothing happens.
I was able to reproduce it. It seems that ContextMenu closes while trying to open it, but mouse capture is not released so it doesn't interact with the rest of the View anymore.
Could you please report this one too?

Thanks for your cooperation.
 
nikobarli
Topic Author
Posts: 186
Joined: 26 Apr 2017, 06:23

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

20 Sep 2017, 03:28

I filed the issues: #001151 and #001152.
 
User avatar
sfernandez
Site Admin
Posts: 3364
Joined: 22 Dec 2011, 19:20

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

20 Sep 2017, 16:24

Thanks Niko :)
 
AnKor
Posts: 35
Joined: 22 Jul 2022, 16:14

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

23 May 2024, 08:46

Hi! This is an old thread, but I have a similar issue in latest version 3.2.3 - I want to programmatically show context menu from resources, but it either doesn't show or behaves weird.
This can be reproduced if I modify "Buttons" example from SDK like this:

Resources.xaml
    <ContextMenu x:Key="AMenu">
        <MenuItem Header="111"/>
        <MenuItem Header="222"/>
        <MenuItem Header="333"/>
    </ContextMenu>
MainWindow.xaml
<Button x:Name="ExitButton" Grid.Row="5" Content="EXIT" Command="{Binding ExitCommand}"
    CommandParameter="{Binding ContextMenu, RelativeSource={RelativeSource Self}}"
    ContextMenu="{StaticResource AMenu}">
ViewModel.cpp
void ViewModel::Exit(BaseComponent* arg)
{
    DynamicCast<ContextMenu*>(arg)->SetIsOpen(true);
    NS_LOG_INFO("Exit Game");
}
The menu doesn't appear in Noesis, but appears in WPF.

However, it appears correctly If I place the menu inside MainWindow's resources. So it seems the culprit is application resources, but what do they miss?

It also sort of works if I call SetNodeParent on the menu before opening to "attach" it to the button. But things seem to go weird. It is hard to reproduce on minimal example, but we get issues like clicking on non-MenuItem inside the menu (we have a header in its template) causing the menu to close and reopen again. Focus behavior also breaks: the button that shows the menu is inside an engaged control and when I press ESC the menu doesn't close, instead the control is disengaged and focus can now move freely outside the menu that just stays on screen.
Maybe it is something in our code, but it doesn't happen if I open the menu in "natural way" via the right click.

Do you have any advice?
 
User avatar
sfernandez
Site Admin
Posts: 3364
Joined: 22 Dec 2011, 19:20

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

23 May 2024, 10:46

This is a bug, could you please report in our bugtracker?
 
AnKor
Posts: 35
Joined: 22 Jul 2022, 16:14

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

23 May 2024, 13:16

Created #3330
Thanks!

Who is online

Users browsing this forum: Bing [Bot] and 1 guest