aivanov
Topic Author
Posts: 19
Joined: 30 Sep 2024, 17:25

OneWay Binding to ContextMenu IsOpen causes AdornerDecorator Error

15 Nov 2024, 23:06

Hello, I am trying to add a one way binding to the "IsOpen" property on a button's context menu so that the model a few layers up can detect when the context menu is open.

The screen has an ItemsControl with a bunch of buttons and each button has its own context menu. If any context menu is currently open, the model needs to know this in order to correctly handle user button inputs (If the user presses "back" while a context menu is open the screen shouldn't close). Below is the XAML portion of the code. The main bit to note for the model is the "IsContextMenuOpen" boolean which the XAML binds to.
<ScrollViewer>
    <StackPanel>
        <Expander x:Name="Expander" DataContext="{Binding ExpanderModel}">
            <Grid>
                <ItemsControl x:Name="Items" ItemsSource="{Binding Items}" ItemTemplate="{StaticResource ListItemTemplate}" Margin="0,10,0,0" Focusable="False"/>
            </Grid>
            <Expander.Header>
                <TextBlock Text="Header"/>
            </Expander.Header>
        </Expander>
    </StackPanel>
</ScrollViewer>

<DataTemplate x:Key="ListItemTemplate">
    <Grid x:Name="Root">
        <ContentControl Grid.Column="0" Template="{DynamicResource MenuTemplate}" Content="{Binding}"/>
    </Grid>
</DataTemplate>

<ControlTemplate x:Key="MenuTemplate">
    <Grid x:Name="Root">
        <Button x:Name="TemplateButton" Focusable="True">
            <Button.ContextMenu>
                <ContextMenu IsOpen="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer}, Path=DataContext.IsContextMenuOpen, Mode=OneWayToSource}">
            </Button.ContextMenu>
        </Butto>
    </Grid>
</ControlTemplate>
The binding links correctly without any compilation errors and the UI is able to open without issue at runtime. The first time the context menu is opened for a given button the boolean value on the model is correctly updated and there are no errors. However, when the button's context menu is opened a second time the following runtime error occurs:
[NOESIS] AdornerDecorator.GetVisualChild(1) returned child (AdornerLayer) with a different visual parent (null)
Interestingly, there are no AdornerDecorator elements anywhere in the project.

Why might this error be happening? Is there a better way for the model to determine if a context menu is currently open?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: OneWay Binding to ContextMenu IsOpen causes AdornerDecorator Error

18 Nov 2024, 11:53

This looks like a bug in our code, nothing wrong on your side.
Could you please report it in our bugtracker and we will fix it?
 
aivanov
Topic Author
Posts: 19
Joined: 30 Sep 2024, 17:25

Re: OneWay Binding to ContextMenu IsOpen causes AdornerDecorator Error

18 Nov 2024, 14:54

Who is online

Users browsing this forum: Bing [Bot] and 5 guests