After migrating to VB.NET Core 9.0 from 8.0, I am getting error messages on menu bindings

Mike van der Waard 0 Reputation points
2024-12-07T10:11:36.7266667+00:00

After migrating from version 8.0 to version 9.0, I am getting error messages related to the binding of menu items. I am working with Visual Studio 17.12.3 and .NET Core 9.0.101 on a Windows 11 (23H2) machine. Further down in the XAML code, I have the same binding in a button, which does not generate any errors. The XAML code is from a WPF project built using MVVM, with no code-behind. When I revert to version 8.0, everything works fine again. Is something changed for the menuitems?

The ERROR is: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext.BttnLeeg; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')

The code that works (RelativeSource Binding of the Button):

 <Button  Height="18" Width="18" HorizontalAlignment="Right" 
                             ToolTip="{Binding DataContext.BttnRemoveConnection_ToolTip, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                             Command="{Binding DataContext.BttnRemoveConnection,         RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"  
                             CommandParameter="{Binding RetourValue}"
                             Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ShowBttn}">
     <DockPanel          LastChildFill="True" Width="18" Height="18">
         <TextBlock      Text="{Binding DataContext.BttnRemoveConnection_Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"      Style="{StaticResource RemoveLink_Background}" Margin="0,0,0,0"  />
         <TextBlock      Text="{Binding DataContext.BttnRemoveConnection_Content_Info, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Style="{StaticResource RemoveLink_Foreground}" Margin="-16,-4,0,0"  />
     </DockPanel>
 </Button>

The code with the ERROR (just one menu item, of the 5 in the original code):

 <UserControl.Resources>
     <ContextMenu x:Key="MyContextMenu" x:Shared ="true" Background="DarkGray" Foreground="Black" >
         <MenuItem Header="Leeg" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.BttnLeeg}" CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" Height="24" />
     </ContextMenu>
 </UserControl.Resources>
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
768 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,805 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,760 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
824 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 3,645 Reputation points Microsoft Vendor
    2024-12-09T09:07:30.6166667+00:00

    Hi,@Mike van der Waard. Welcome to Microsoft Q&A. 

    You could refer to the following method to fix your problem.

    When the resources defined in Resources need to be bound to DataContext, define DataContext in Resources for other controls to perform data binding.

    Reference code:

    <UserControl.Resources>
        <local:UserControl1ViewModel x:Key="UserControl1ViewModelInResource"> </local:UserControl1ViewModel>
            <ContextMenu x:Key="MyContextMenu" x:Shared ="true" Background="DarkGray" Foreground="Black" DataContext="{StaticResource UserControl1ViewModelInResource}">
                <MenuItem Header="Leeg" Command="{Binding BttnLeeg}" CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" Height="24" />
            </ContextMenu>
    </UserControl.Resources>
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.