代码之家  ›  专栏  ›  技术社区  ›  L-Four

在DataTemplate中绑定ContextMenu

  •  1
  • L-Four  · 技术社区  · 14 年前

    我有:

     <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中的命令绑定到viewmodel中定义的I command,viewmodel是listbox的datacontext,commandparameter应该是StyleViewModel,但我尝试的没有成功。有人能指点我正确的方向吗?

    2 回复  |  直到 14 年前
        1
  •  5
  •   L-Four    14 年前

    找到了!

    <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>
    
        2
  •  1
  •   L-Four    14 年前

    现在几乎可以工作了,除了now 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>
    

    我想知道是否可以。。。

    推荐文章