从ContextMenu绑定到父DataContext

时间:2018-03-20 10:12:52

标签: c# wpf xaml data-binding

我正在努力从DataTemplate / ContextMenu控件绑定到父DataContext。我已经尝试过提及herehere的解决方案,但没有运气。我无法同时绑定到属性或命令 - 不抛出任何异常,但不调用命令并将属性设置为null。

这是我的代码示例(尽可能简化):

<Metro:MetroWindow ...>
<Window.DataContext>
    <local:MyViewModel />
</Window.DataContext>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="10*" />
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>
    <StackPanel ... />
    <Grid Grid.Column="1">
        <Border ... />
        <ListBox x:Name="FileList" ItemsSource="{Binding AddedFiles}" Margin="5,5,5,5" SelectionMode="Multiple" SelectionChanged="ItemsSelectionChanged">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel MaxWidth="700" IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type local:FileListItem}">
                    <Grid Margin="5" Width="110" ToolTip="{Binding Path=TooltipInfo}" MouseDown="FileItemClick" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}">
                        <Grid.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="{Binding PlacementTarget.Tag.test,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" />
                                <MenuItem Header="Test command" Command="{Binding PlacementTarget.Tag.CloseCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                            </ContextMenu>
                        </Grid.ContextMenu>
                        <Grid.RowDefinitions ... />
                        <StackPanel ...(Databinding works here!) />
                        <TextBlock ... (Databinding works here!) />
                        <Rectangle ... (Databinding works here!) />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

我也尝试通过RelativeSource将Tag属性与Window的DataContext绑定(以及其他主题中找到的其他可能的解决方案),但没有任何反应。在我的方法中,某些事情必定是明显错误的。

2 个答案:

答案 0 :(得分:2)

您无法在Grid中找到PlacementTarget属性。像这样更改你的绑定,

 <MenuItem Header="{Binding PlacementTarget.Tag.test,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" />

或者喜欢,

<MenuItem Header="{Binding Parent.PlacementTarget.Tag.test,RelativeSource={RelativeSource Self}}" />

另外要访问ViewModel的命令,ListBox的Access DataContext而不是ListBoxItem,

 <Grid Margin="5" Width="110" ToolTip="{Binding Path=TooltipInfo}" MouseDown="FileItemClick" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode= FindAncestor, AncestorType=ListBox}}">

答案 1 :(得分:0)

你试过这个吗? (注意 DataContext 已添加)

<MenuItem Header="{Binding DataContext.PlacementTarget.Tag.test,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}}" />