Bug 42999 - MenuItems in ContextActions are sometimes bound to the wrong bindingContext in UWP.
Summary: MenuItems in ContextActions are sometimes bound to the wrong bindingContext i...
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-08-02 15:26 UTC by Jacco Dieleman
Modified: 2017-11-29 06:25 UTC (History)
5 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:


Attachments
Simple solution with which the problem can be reproduced. (Binding of UWP ContextActions) (129.03 KB, application/x-zip-compressed)
2016-08-02 15:26 UTC, Jacco Dieleman
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 42999 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Jacco Dieleman 2016-08-02 15:26:12 UTC
Created attachment 16866 [details]
Simple solution with which the problem can be reproduced. (Binding of UWP ContextActions)

Problem
-------
In UWP the MenuItems in ContextActions are sometimes bound to the wrong bindingContext.
E.g. We have items 1,2,3,4 and 5 in a ListView. When I long press item 5 the contextmenu appears. The bindingContext of the MenuItem is item 3.

Reproduction
------------
Run the app in the attachement.
Excute the contextAction for item 3.
Add a new item (5) to the list by clicking the button on the top of the page.
Excute the contextAction for item 5. Notice that it effects item 3 instead of item 5!

Notes
-----
- The problem seems to be caused by updating item 3 in the ListView with a new item while we are executing its contextmenu. In case of the reproduction project I can solve the problem by implementing an OnPropertyChanged for the IsRead property of the item. In the real world we need to reload the item from SQLite when the contextmenu is executed so that solution will not work.
- This is not a problem on Android.
Comment 1 damian 2017-06-19 11:23:49 UTC
The underlying cause seems to be that the DataContext of the FlyoutItem is only set when the FlyoutItem is first created by SetupMenuItems (invoked when the list of ContextActions changes or when the context menu is first displayed).

It is not subsequently updated when the data context changes.

My workaround is to define a Custom `CellControl` in my Xamarin Forms UWP project:

```
public class CustomCellControl : CellControl {
    public CustomCellControl() {
      DataContextChanged += CustomCellControl_DataContextChanged;
    }

    private void CustomCellControl_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args) {
      var flyout = FlyoutBase.GetAttachedFlyout(CellContent) as MenuFlyout;
      if (flyout?.Items == null) return;
      foreach (var flyoutItem in flyout.Items) {
        var menuItem = (MenuItem) flyoutItem.DataContext;
        menuItem.BindingContext = args.NewValue;
      }
    }
  }
```

And then wire it up in my Xamarin Forms UWP project's App.xaml:

```xml
<Application
  x:Class="TBS.UWP.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:uwp="using:Xamarin.Forms.Platform.UWP"
  xmlns:uwp1="usingTBS.UWP"
  RequestedTheme="Light">
  <Application.Resources>
    <ResourceDictionary>
	    <DataTemplate x:Key="CellTemplate">
				<uwp1:CustomCellControl HorizontalContentAlignment="Stretch" Height="{Binding Cell.RenderHeight,RelativeSource={RelativeSource Mode=Self},Converter={StaticResource HeightConverter}}" />
	    </DataTemplate>
```
Comment 2 damian 2017-06-19 11:33:00 UTC
You'll need to add a null check on CellContent in CustomCellControl_DataContextChanged
Comment 3 OBATA Akio 2017-11-22 08:35:36 UTC
Context Menu must be completely regenerated after DataContext is changed, instead of just replace MenuItem's BindingContext if already exists, for the case using DataTemplateSelector.
Comment 4 OBATA Akio 2017-11-29 06:25:58 UTC
Probably, SetupContextMenu() in CellControl should always do FlyoutBase.SetAttachedFlyout(CellContent, null).