Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
45
Bug: xamContextMenu null reference exception in XamMenuItem.RaiseClickEvent()
posted

Hi,

we have NetAdvantage for WPF (v2011.2). I can't use the xamContext menu because a null reference exception is thrown if the menuItem is clicked. 

I tried Service Release 2, and hoped that this bug has been fixed "Element hosted menu throwing NullReferenceException during GetClickedElements method." But it didn't help. Same behavior as before.

 

Here is the stacktrace:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Infragistics.Controls.Menus.XamMenuItem.RaiseClickEvent()
   at Infragistics.Controls.Menus.XamMenuItem.HandleItemSelection()
   at Infragistics.Controls.Menus.XamMenuItem.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.ContentElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

 

Our xaml is:

 <DataTemplate x:Key="TreeItemTemplate">
      <StackPanel Orientation="Horizontal" Margin="-3">
        <!--StackPanel.ContextMenu>
          <ContextMenu Visibility="{Binding Path=Data.ContextMenu.Visibility}"
                     ItemsSource="{Binding Path=Data.ContextMenu.MenuItems}"/>
        </StackPanel.ContextMenu-->
        <!-- xamContextMenu bug? null reference exception on Click event! -->
        <ig:ContextMenuService.Manager>
        <ig:ContextMenuManager>
          <ig:ContextMenuManager.ContextMenu>
            <ig:XamContextMenu Visibility="{Binding Path=Data.ContextMenu.Visibility}"
                               ItemsSource="{Binding Path=Data.ContextMenu.MenuItems}"
                               HierarchicalItemTemplate="{StaticResource MenuItemTemplate}"
                               ItemContainerStyleSelector="{StaticResource menuSeparatorStyleSelector}"
                               />
          </ig:ContextMenuManager.ContextMenu>
        </ig:ContextMenuManager>
      </ig:ContextMenuService.Manager>
        <Image Name="groupImg" Width="16" Height="14" Source="{Binding Data.GroupIcon}" VerticalAlignment="Center" />
        <Image Width="16" Height="14" Margin="3" Source="{Binding Data.Icon}" VerticalAlignment="Center" />
        <TextBlock Name="nodeTxt" Text="{Binding Data.Text}" Foreground="{Binding Data.TextColor}" FontWeight="{Binding Data.FontWeight}" VerticalAlignment="Center"/>
      </StackPanel>
    <DataTemplate.Triggers>
      <!-- Only show group icon if not null -->
      <DataTrigger Binding="{Binding Data.GroupIcon}" Value="{x:Null}">
        <Setter TargetName="groupImg" Property="Visibility" Value="Collapsed" />
      </DataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>

 

Please let me know if you need more input. Thanks in advance.

  • 45
    posted in reply to Kai

    This is how it should be shown (using legacy WPF menu). I can also directly bind to my  ICommand from within the MenuItem style, independently of the menu level:

     

     <!-- legacy Menu -->
     
      <HierarchicalDataTemplate
            DataType="{x:Type VM:MenuItemViewModel}"
            ItemsSource="{Binding Path=MenuItems}">
      </HierarchicalDataTemplate>

      <Style TargetType="{x:Type MenuItem}">
        <Setter Property="MenuItem.Header" Value="{Binding Path=Text}"/>
        <Setter Property="MenuItem.Command" Value="{Binding Path=Command}"/>
        <Setter Property="MenuItem.Icon" Value="{StaticResource MenuImg}"/>
        <Setter Property="MenuItem.Visibility" Value="{Binding Visibility}" />
        <Setter Property="MenuItem.IsCheckable" Value="{Binding IsCheckable}" />
        <Setter Property="MenuItem.IsChecked" Value="{Binding IsChecked}" />
        <Style.Triggers>
          <!-- if MenuItemViewModel is null, then show the separator -->
          <DataTrigger Binding="{Binding}" Value="{x:Null}">
            <Setter Property="Template" >
              <Setter.Value>
                <ControlTemplate>
                  <Separator Style="{StaticResource {x:Static MenuItem.SeparatorStyleKey}}"/>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </DataTrigger>
        </Style.Triggers>
      </Style>

      <Style TargetType="{x:Type MenuItem}" x:Key="menuSeparatorStyle1">
        <Setter Property="Template" >
          <Setter.Value>
            <ControlTemplate>
              <Separator Style="{StaticResource {x:Static MenuItem.SeparatorStyleKey}}"/>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>

     

  • 45
    posted in reply to [Infragistics] Krasimir

    Hi Krasimir,

    thank you. This workaround did it :). I just  reassigned ItemsSource in the Loaded event handler:

    ctxMenu.ItemsSource = ctxMenu.ItemsSource;

     

    Now I have the next problem. Please see below a screenshot of my context menu. I want to apply the same DataTemplate to the first menu level and for the child menu level. That means show my icon, text and bind to the ICommand of my menuitem's view model and so on.

    I don't get this working. I can only apply my template to the "content" of the first level menu items, but not to the xamMenuItem self.

    I use the DefaultItemsContainer of the xamMenu, but it seems that this applies only to the menuitem childs :(

    Before, I did some experiments with styling the xamMenuItem. But this overwrites the style of my (dynamically loaded) theme and the ICommand binding didn't work.

    Can you help me? Thanks.

     

     

    Here is our xaml:

     

    <!--
       Menu / Context Menu
      -->

      <Image x:Key="MenuImg" Height="16" Width="16" VerticalAlignment="Center"
                                Source="{Binding Icon}" x:Shared="false"/>

      <DataTemplate x:Key="MenuTextTemplate">
        <AccessText Text="{Binding Text}"/>
      </DataTemplate>

      <!-- Xam Menu -->

      <DataTemplate x:Key="XamMenuItemTemplate">
        <ig:XamMenuItem Visibility="{Binding Visibility}"
                            IsCheckable="{Binding IsCheckable}"
                            IsChecked="{Binding IsChecked, Mode=TwoWay}"
                            IsEnabled="{Binding IsEnabled}"
                            ToolTip="{Binding ToolTip}"
                            Icon="{StaticResource MenuImg}">
          <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
              <i:InvokeCommandAction Command="{Binding Command}" />
            </i:EventTrigger>
          </i:Interaction.Triggers>
        </ig:XamMenuItem>
      </DataTemplate >

      <Style TargetType="{x:Type ig:XamMenuItem}"
             x:Key="menuSeparatorStyle">
        <Setter Property="Template" >
          <Setter.Value>
            <ControlTemplate>
              <ig:XamMenuSeparator/>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>

      <DataTemplate x:Key="MenuItemTemplate1">
        <StackPanel Orientation="Horizontal">
          <AccessText Text="Level 1: "/>
          <AccessText Text="{Binding Text}"/>
        </StackPanel>
      </DataTemplate>

      <ig:HierarchicalDataTemplate x:Key="MenuItemTemplate"
            ItemsSource="{Binding Path=MenuItems}"
            ItemTemplate="{StaticResource MenuTextTemplate}"
            DefaultItemsContainer="{StaticResource XamMenuItemTemplate}"
            Template="{StaticResource MenuItemTemplate1}" />

      <DataTemplate x:Key="TreeItemTemplate">
        <StackPanel Orientation="Horizontal" Margin="-3">
          <ig:ContextMenuService.Manager>
            <ig:ContextMenuManager>
              <ig:ContextMenuManager.ContextMenu>
                <ig:XamContextMenu Visibility="{Binding Path=Data.ContextMenu.Visibility}"
                                   ItemsSource="{Binding Path=Data.ContextMenu.MenuItems}"
                                   HierarchicalItemTemplate="{StaticResource MenuItemTemplate}"
                                   ItemContainerStyleSelector="{StaticResource menuSeparatorStyleSelector}"
                                   Loaded="XamContextMenu_Loaded" />
              </ig:ContextMenuManager.ContextMenu>
            </ig:ContextMenuManager>
          </ig:ContextMenuService.Manager>
          <Image Name="groupImg" Width="16" Height="14" Source="{Binding Data.GroupIcon}" VerticalAlignment="Center" />
          <Image Width="16" Height="14" Margin="3" Source="{Binding Data.Icon}" VerticalAlignment="Center" />
          <TextBlock Name="nodeTxt" Text="{Binding Data.Text}" Foreground="{Binding Data.TextColor}" FontWeight="{Binding Data.FontWeight}" VerticalAlignment="Center"/>
        </StackPanel>
        <DataTemplate.Triggers>
          <!-- Only show group icon if not null -->
          <DataTrigger Binding="{Binding Data.GroupIcon}" Value="{x:Null}">
            <Setter TargetName="groupImg" Property="Visibility" Value="Collapsed" />
          </DataTrigger>
        </DataTemplate.Triggers>
      </DataTemplate>

  • 30945
    Offline posted

    Hello,

     

    I have logged this behavior with our developers in our tracking system, with an issue ID of 108776.  I have also created a support ticket on your behalf with number CAS-89066-FKYXXL in order to link the development issue to it so that you are automatically updated when a Service Release containing your fix is available for download.

     

    Also you can avoid this behavior by setting the ItemsSource property of the XamContextMenu in the Loaded event of the control. I have created a sample application for you, which demonstrates how you can implement the mentioned approach.

     

    Sincerely,

    Krasimir

    Developer Support Engineer

    Infragistics, Inc.

    www.infragistics.com/support

    ContextMenuNullException_1.zip