在DataTemplate中绑定ContextMenu

时间:2010-11-17 19:24:40

标签: .net binding tags contextmenu datatemplate

我有:

 <ListBox>
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type ViewModels:StyleViewModel}">
                    <DockPanel>                            
                        <Button Content="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
                            <Button.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Delete" Command="{Binding PlacementTarget.Tag.DataContext.RemoveMember1FavoriteStyleCommand}" CommandParameter="{Binding}" />
                                </ContextMenu>
                            </Button.ContextMenu>
                        </Button>                            
                    </DockPanel>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>

我想要实现的是将上下文菜单的menuitem中的命令绑定到在视图模型中定义的ICommand,该视图模式是列表框的datacontext,而命令参数应该是StyleViewModel,但是我是什么试过没办法。有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:5)

发现它!

<ListBox ItemsSource="{Binding ActiveCustomer.Member1FavoriteStyles}" ItemsPanel="{StaticResource ListBoxStyleItemsPanelAsVerticalStackPanel}" ItemContainerStyle="{StaticResource ListBoxItemContainerStyle}" Background="Transparent" BorderThickness="0">
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type ViewModels:StyleViewModel}">
                    <DockPanel>                            
                        <Button Content="{Binding Name}" Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
                            <Button.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Remove" Command="{Binding PlacementTarget.Tag.RemoveMember1FavoriteStyleCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandParameter="{Binding}" />
                                </ContextMenu>
                            </Button.ContextMenu>
                        </Button>                            
                    </DockPanel>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>

答案 1 :(得分:1)

现在几乎正常工作,除了现在CommandParameter =“{Binding}”没有返回StyleViewModel:

 <ListBox ItemsSource="{Binding ActiveCustomer.Member1FavoriteStyles}" ItemsPanel="{StaticResource ListBoxStyleItemsPanelAsVerticalStackPanel}" ItemContainerStyle="{StaticResource ListBoxItemContainerStyle}" Background="Transparent" BorderThickness="0">
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type ViewModels:StyleViewModel}">
                    <DockPanel>
                        <Button Content="{Binding Name}" Tag="{Binding DataContext,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
                            <Button.ContextMenu>
                                <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.Tag}">
                                    <MenuItem Header="{Binding ActiveCustomer.Member1FirstName}" Command="{Binding RemoveMember1FavoriteStyleCommand}" CommandParameter="{Binding}" />
                                </ContextMenu>
                            </Button.ContextMenu>
                        </Button>                            
                    </DockPanel>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>

我想知道是否可以做到......