从上下文菜单绑定到父级DataContext

3
我在从DataTemplate/ContextMenu控件绑定到父级DataContext方面遇到了问题。我已经尝试了这里这里提到的解决方案,但是没有成功。我无法绑定到属性或命令 - 没有抛出异常,但命令未被调用,属性设置为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属性与窗口的DataContext绑定(以及其他在其他话题中找到的可能解决方案),但并没有任何效果。我的方式显然出了问题。


这个命令CloseCommand在FileListItem中吗? - WPFUser
@WPFUser 不是的,它在ViewModel(父DataContext)中。这就是想法-使用单个FileListItem的ContextMenu从父DataContext调用命令。 - baka1408
1
你应该访问 ListBox 的 DataContext,而不是它的 Item DataContext。 - WPFUser
2个回答

6
您在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的DataContext而不是ListBoxItem。
 <Grid Margin="5" Width="110" ToolTip="{Binding Path=TooltipInfo}" MouseDown="FileItemClick" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode= FindAncestor, AncestorType=ListBox}}">

访问ListBoxItem的DataContext而不是ListBox是问题的原因 - 我没有注意到与Grid相关联会给出单个列表项上下文。非常感谢! - baka1408

0

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

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

很遗憾,这并没有改变什么,但还是谢谢你的回复。 - baka1408

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接