在 DataTemplate 中绑定 ContextMenu

发布于 2024-10-03 04:36:21 字数 1124 浏览 2 评论 0原文

我有:

 <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>

我想要实现的是将上下文菜单的菜单项中的命令绑定到在视图模型中定义的 ICommand,该视图模型是列表框的数据上下文,并且命令参数应该是 StyleViewModel,但是我尝试过没有成功。有人能指出我正确的方向吗?

I have:

 <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>

What I'm trying to achieve is to bind the command in the menuitem of the context menu to an ICommand that is defined in a viewmodel that is the datacontext of the listbox, and the commandparameter should be the StyleViewModel, but what I tried didn't work. Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2024-10-10 04:36:21

找到了!

<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>

Found it!

<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>
并安 2024-10-10 04:36:21

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

我想知道是否可以完成......

Almost working now, except that now CommandParameter="{Binding}" is not returning the 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>

I'm wondering if it can be done...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文