nikobarli
Topic Author
Posts: 180
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: 180
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: 180
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: 2984
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: 180
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: 2984
Joined: 22 Dec 2011, 19:20

Re: Cannot show ContextMenu programmatically when instantiating it from Resources

20 Sep 2017, 16:24

Thanks Niko :)

Who is online

Users browsing this forum: No registered users and 6 guests