开发者

DataBinding Oddity - Converter not called on the second pass

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}" />
                </ContextMen开发者_开发百科u>
</DataGrid.ContextMenu>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜