views:

26

answers:

1

I have a context menu that is defined as a resource and bound to the SelectedItem in a DataGrid, using a converter to get the display name of the current item, as in "Edit " or "Edit "

It works fine for the first selected item, but doesn't call the converter on the second (I have a break point in it that only gets hit on the first pass). BUT if I invoke the bound command on the second pass, it invokes on the newly selected instance as it should.

I use this technique with other ItemsControls and I can't spot anything wrong. Any ideas?

Cheers,
Berryl

Resource & binding

<ContextMenu x:Key="ProjectActivityContextMenu" x:Shared="true">
    ...
    <MenuItem Header="{Binding SelectedProjectActivity, Converter={StaticResource DeleteProjectConv}}" Command="{Binding DeleteCommand}" />
</ContextMenu>

Wiring

<DataGrid ...
    SelectedItem="{Binding SelectedProjectActivity}" 
    IsSynchronizedWithCurrentItem="True" 
    ContextMenu="{DynamicResource ProjectActivityContextMenu}"
 >

UPDATE

This also happens if I declare it as part of the grid, like:

<DataGrid.ContextMenu>
    <ContextMenu >
    ....
    <MenuItem Header="{Binding SelectedProjectActivity, Converter={StaticResource DeleteProjectConv}}" Command="{Binding DeleteCommand}" />
                </ContextMenu>
</DataGrid.ContextMenu>
A: 

I think what you're seeing is just a consequence of defining the ContextMenu as a Resource. Until you actually open the menu, it has no DataContext to apply the Bindings to so the converter isn't being called. This should hold even if you change the selection a few times without opening the menu. Once you right click and open the menu the DataContext is applied and the Bindings can be resolved.

John Bowen
@John. Thanks for the reply, but it also happens when the menu is declared as part of the grid, as in the end of my updated post.
Berryl
The answer was really to trigger a property change, not changing the location of the resource.
Berryl