ContextMenu Closes Immediately

1.5k views Asked by At

Total Edit:

Now I can reproduce my error and I really don't know why it happens. Any help very much appreciated.

I have the following UI:

<Canvas>
    <Button Content="Remove and Readd Rectangle" Click="Button_Click"/>
    <ItemsControl ItemsSource="{Binding Rectangles}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle Width="{Binding Width}" Height="{Binding Height}" Stroke="Black" Fill="{Binding Color}" MouseUp="Rectangle_MouseUp" >
                    <Rectangle.Resources>
                        <RaAtt:RaAttached_BindingProxy Data="{Binding}" x:Key="Data"/>
                    </Rectangle.Resources>
                    <Rectangle.ContextMenu>
                        <ContextMenu IsOpen="{Binding Data.IsOpen, Source={StaticResource Data}}">
                            <MenuItem Header="Test"/>
                        </ContextMenu>
                    </Rectangle.ContextMenu>
                </Rectangle>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Canvas>

Now I want to open a ContextMenu via a MouseClick on the Rectangle:

private void Rectangle_MouseUp(object sender, MouseButtonEventArgs e)
{
    ((sender as Rectangle).DataContext as MyRectangle).IsOpen = true;
    e.Handled = true;
}

It works on every single Rectangle.

When I click the Button I remove and immediately readd a Rectangle:

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyRectangle x = Rectangles.FirstOrDefault();
    Rectangles.Remove(x);
    Rectangles.Insert(0, x);
}

If I do this on this removed and readded rectangle the ContextMenu opens and immediately closes again.

Focus log:

Start Application:

'KeyboardFocus:' TestNamespace.MainWindow

RightClick:

'KeyboardFocus:' System.Windows.Controls.ContextMenu Items.Count:1
'KeyboardFocus:' TestNamespace.MainWindow

Click on Button:

'KeyboardFocus:' System.Windows.Controls.Button: Remove and Readd Rectangle

RightClick:

'KeyboardFocus:' System.Windows.Controls.ContextMenu Items.Count:1
'KeyboardFocus:' System.Windows.Controls.Button: Remove and Readd Rectangle
'KeyboardFocus:' System.Windows.Controls.ContextMenu Items.Count:1 //Why does this here happen?
'KeyboardFocus:' System.Windows.Controls.Button: Remove and Readd Rectangle

Does anyone know why?

Thank you so much!

2

There are 2 answers

0
Noman Khan On

Did you try and debug, maybe there is another UI Element being set to receive focus programmatically which will cause the Context Menu to close? Has happened to me before.

Check the Call Stack.. set some breakpoints..

3
Alexander Bell On

You did not include enough of critical info pertinent to the app functionality in your question (instead, plenty of irrelevant XAML formatting stuff), so the answer is sort of generic: you have to look into code-behind mouse event handlers, namely;

Rectangle_MouseUp and Rectangle_MouseDown.

Hope this may help.