This question is locked. New answers and comments are not allowed.
I have a ContextMenu on a JumpList with multiple context menu entries.
A RelayCommand (from the MVVM Light Toolkit) is bound to their Command property and this command also has CanExecute logic attached.
Within the CanExecute handling of the MVVM Light toolkit I started to get InvalidCastExceptions which I could not explain. So I downloaded the MVVM Light source code and looked what happens.
It looks like sometimes the context menu passes in some random other controls like Border or TextBox as the CanExecute parameter instead of the RadDataBoundListBoxItem.
I only could reproduce this behavior when CanExecute returns false, meaning the ContextMenu item is disabled at the moment. But also in this case, it only appears if I open the context menu for a 2nd time while items are disabled. On the 1st time, it works and all items are shown disabled as expected. If CanExecute returns true I can open the ContextMenu as often as I want and this exception does not occur.
I attached a screenshot showing you how the CanExecute method gets called when this exception appears. Notice that the CanExecute type is based on the RadDataBoundListBoxItem (as this is what I have defined in the RelayCommand and what I would expect to always get as a parameter from the ContextMenu binding) whereas the passed parameter is of type System.Windows.Controls.Border ...
Please have a look at this. I can again send you my project in a support ticket if this helps you finding the issue, please let me know.
StackTrace:
Edit:
I can workaround this issue if I use just object instead of RadDataBoundListBoxItem for the generic RelayCommand declaration like this:
Then I can work around the exception within the RelayCommand but I have to do some casting and type checking myself which I want to avoid, so it would be great if this could be fixed.
A RelayCommand (from the MVVM Light Toolkit) is bound to their Command property and this command also has CanExecute logic attached.
Within the CanExecute handling of the MVVM Light toolkit I started to get InvalidCastExceptions which I could not explain. So I downloaded the MVVM Light source code and looked what happens.
It looks like sometimes the context menu passes in some random other controls like Border or TextBox as the CanExecute parameter instead of the RadDataBoundListBoxItem.
I only could reproduce this behavior when CanExecute returns false, meaning the ContextMenu item is disabled at the moment. But also in this case, it only appears if I open the context menu for a 2nd time while items are disabled. On the 1st time, it works and all items are shown disabled as expected. If CanExecute returns true I can open the ContextMenu as often as I want and this exception does not occur.
I attached a screenshot showing you how the CanExecute method gets called when this exception appears. Notice that the CanExecute type is based on the RadDataBoundListBoxItem (as this is what I have defined in the RelayCommand and what I would expect to always get as a parameter from the ContextMenu binding) whereas the passed parameter is of type System.Windows.Controls.Border ...
Please have a look at this. I can again send you my project in a support ticket if this helps you finding the issue, please let me know.
StackTrace:
at GalaSoft.MvvmLight.Command.RelayCommand`1.CanExecute(Object parameter)
at Telerik.Windows.Controls.RadContextMenuItem.GetActualCommandParameter()
at Telerik.Windows.Controls.RadContextMenuItem.OnLoaded(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
Edit:
I can workaround this issue if I use just object instead of RadDataBoundListBoxItem for the generic RelayCommand declaration like this:
// Instead of this
public RelayCommand<
RadDataBoundListBoxItem
> MoveFeedContextCommand { get; private set; }
// Use that
public RelayCommand<
object
> MoveFeedContextCommand { get; private set; }
Then I can work around the exception within the RelayCommand but I have to do some casting and type checking myself which I want to avoid, so it would be great if this could be fixed.