0

这是一个简单的问题。我有一个 XAML,目前看起来像这样:

<ListBox>
     <ListBox.ContextMenu>
            <ContextMenu DataContext="{Binding Path=FeedContextMenu}"
     </ListBox.ContextMenu>
</ListBox>

My intention is to take the data context from the ListBox and use its FeedContextMenu property as a DataContext for ContextMenu. Now all you WPF gurus will probably immediately say that this will not work. Apparently, this has something to do with the fact that ContextMenu isn't part of the visual tree. Now I'm not a WPF expert, so after googling for hours and trying out different suggested solutions that didn't work nor make any sense to me whatsoever, I would like to ask someone with a greater knowledge to explain it to me what and why needs to be done in order for this to work. Thanks.

4

1 回答 1

4

您可以尝试绑定到 ContextMenu 的PlacementTarget(即 ListBox)的 DataContext,如下所示:

<ContextMenu DataContext="{Binding PlacementTarget.DataContext.FeedContextMenu,
                                   RelativeSource={RelativeSource Self}}">
于 2013-01-21T12:46:08.010 回答