4
Vote

ContextMenu sender has wrong data.

description

The ContextMenu seems to be doing some strange things.

An example is if I have a LongListSelector with 4 items in it. If I tap and hold on item 4, I'll get the datacontext of that item. If I then tap and hold on item 1, I still get the datacontext of item 4. It gets stranger ... if I tap and hold on item 1 again, I'll then get the datacontext of item 1.
This will happen every time if opening the context menu for the first and last items in the LLS. All the items between are usually OK, but sometimes they do the same.

Everything works fine and dandy up until the contextmenu takes effect.

comments

Jaymondo wrote Aug 2, 2013 at 6:03 PM

Found a working fix - for me.
http://stackoverflow.com/questions/14444583/contextmenu-in-datatemplate-binding-issue?rq=1

Involves clearing the datacontext when the ContextMenu is unloaded from the page.

shawnoster wrote Aug 2, 2013 at 10:24 PM

Hey Jay, do you happen to have a sample showing the issue? Also are you using the LLS from the WP7 Toolkit or the native LLS in the WP8 SDK?

I've created a very naïve sample using the WP8 LLS + ContextMenu with only 4 items and am unable to repro this issue so I'm wondering if this is restricted to WP7 LLS. A small repro will help us narrow this issue down.

Thanks

Jaymondo wrote Aug 3, 2013 at 3:15 PM

Hi Shawnoster,

I'll see if I can replicate the issue in a quick app. The app I was having the issue with has been submitted to the marketplace.
I'm using the native LLS with the WP8 SDK

DaveWh wrote Aug 11, 2013 at 10:32 PM

I have a variation on this problem. For me, I can properly use the context menu for several items in the native WP8 LLS. However, it shortly starts giving me randomly incorrect DataContext values. Here's the kicker. If I sit and wait and do nothing else, it'll self-correct. I have not tried the workaround shown above.

App that exhibits the problem: http://www.windowsphone.com/en-us/store/app/remote-guide/665ebeed-e3db-4f37-9a82-afbfd79b6016

Just open the guide page and tap-hold on channels and select to remove them. After several successes, it'll start failing. It fails because the DataContext value being sent to the Click handler is for a LLS item that's been removed!

DaveWh wrote Aug 11, 2013 at 10:44 PM

Yay! The workaround worked! I added an Unloaded event handler for the context menu and explicitly cleared the DataContext property:
    private void ContextMenu_Unloaded(object sender, RoutedEventArgs e)
    {
        ContextMenu contextMenu = sender as ContextMenu;

        contextMenu.ClearValue(FrameworkElement.DataContextProperty);
    }

wrote Sep 8, 2013 at 10:16 PM

wrote Nov 6, 2013 at 2:37 PM

zubairahmed_ wrote Nov 9, 2013 at 6:48 AM

It works.
Thanks DaveWh!

wrote Jun 3, 2014 at 5:47 AM

iakwvina wrote Dec 5, 2014 at 12:49 PM

DaveWh you are my hero!!! After 5-6 hours searching how to solve my problem, you helped me fix it :)
Thanks!!