1

I have a XAML based ContextMenu bound to the rows in a datagrid. It works just fine - until the grid is scrolled!

This is the context menu for one of the controls in the visual tree or a DataGrid row.

<data:DataGridTemplateColumn Header="Customer Details" Width="*">
    <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid Background="Transparent"> <!-- allows click in entire cell -->

               <controlsInputToolkit:ContextMenuService.ContextMenu>
                <controlsInputToolkit:ContextMenu>

                    <controlsInputToolkit:MenuItem Header="{Binding CompletedOrderId,StringFormat='Create Reminder for order #\{0\}'}"  
                                       CommandParameter="{Binding}">
                    <controlsInputToolkit:MenuItem.Command>
                        <command:CreateReminderCommand/>
                    </controlsInputToolkit:MenuItem.Command>
                    <controlsInputToolkit:MenuItem.Icon>
                        <Viewbox>
                        <Image Width="19" Height="18" Source="../images/reminders.png" VerticalAlignment="Center"/>
                        </Viewbox>
                    </controlsInputToolkit:MenuItem.Icon>
                    </controlsInputToolkit:MenuItem>

                <controlsInputToolkit:ContextMenu>
                 <controlsInputToolkit:ContextMenuService.ContextMenu>
                 ......

The ICommand is CreateReminderCommand and the CommandParameter is bound to the data item for the row itself.

This works just fine - I can right click on a row and it will show me the correct text in the menu item 'Create Reminder for order 12345'.

Then I scroll the datagrid down a page. If I keep right clicking on items then suddenly I'll see the wrong order number for a row. I think what must be happening is this :

  • The DataGrid is reusing instances of MenuItem that it has previously created.

How can I force a refresh of the ContextMenu when it is displayed for an item that changes? There's no 'Update method on the ContextMenu or ContextMenuService.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • Can you describe how you manage to get this menu "bound to the data grid row"? – AnthonyWJones May 06 '10 at 08:33
  • its not actually bound to the row itself. its bound to a cell in the DataGrid. I've added a little more XAML to show the templated column in the DataGrid. I couldn't find a way to bind it to the row. Almost did, but theres too much magic going on in the ContextMenuService to let me do it – Simon_Weaver May 06 '10 at 09:31
  • microsoft sent me workaround.. blog entry on subject pending... – Simon_Weaver May 07 '10 at 23:50

1 Answers1

2

This turned out to be a Silverlight bug related to element binding.

http://blogs.msdn.com/delay/archive/2010/05/11/we-ve-secretly-changed-this-control-s-datacontext-let-s-see-if-it-notices-workaround-for-a-silverlight-data-binding-bug-affecting-various-scenarios-including-datagrid-contextmenu.aspx

The solution provided here fixes the problem.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689