WPF命令绑定ItemsControl内的ContextMenu项 [英] WPF Command Binding of ContextMenu Item inside ItemsControl

查看:90
本文介绍了WPF命令绑定ItemsControl内的ContextMenu项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序由一个MainWindow和一个ContentControl组成,我根据所选菜单更改ViewModel.

My Application consists of a MainWindow with a ContentControl and I change the ViewModel depending on the selected menu.

我显示为内容的UserControl之一包含以下WrapPanel:

One of the UserControls I display as content contains the following WrapPanel:

<UserControl ...>
    <Grid>
        <WrapPanel>
            <ItemsControl ItemsSource="{Binding Connections}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Command="{Binding DataContext.ConnectionSelectCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                CommandParameter="{Binding}"
                                FocusManager.FocusedElement="{Binding ElementName=InstanceName}"
                                Style="{DynamicResource DashboardButton}">
                            <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center" Text="{Binding Name}" />
                            <Button.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Delete"
                                              Command="{Binding ConnectionRemoveCommand}"
                                              CommandParameter="{Binding}" />
                                </ContextMenu>
                            </Button.ContextMenu>
                        </Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </WrapPanel>
    </Grid>
</UserControl>

ContextMenu上的Command无效,因为它尝试调用Connection对象上的ConnectionRemoveCommand而不是ConnectionViewModel(即UserControlDataContext).

The Command on the ContextMenu doesn't work because it tries to call ConnectionRemoveCommand on the Connection object instead of the ConnectionViewModel which is the DataContext of the UserControl.

如何如何将Command绑定到ConnectionViewModel,并且CommandParameterConnection对象?

How do I bind the Command to the ConnectionViewModel with the CommandParameter being the Connection object?

推荐答案

如果将ButtonTag属性绑定到ItemsControlDataContext,则可以使用<ContextMenu的c18>:

If you bind the Tag property of the Button to the DataContext of the ItemsControl, you could then bind to it using the PlacementTarget of the ContextMenu:

<Button Command="{Binding DataContext.ConnectionSelectCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
        CommandParameter="{Binding}"
        FocusManager.FocusedElement="{Binding ElementName=InstanceName}"
        Style="{DynamicResource DashboardButton}"
        Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ItemsControl}}">
    <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center" Text="{Binding Name}" />
    <Button.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Delete"
                    Command="{Binding PlacementTarget.Tag.ConnectionRemoveCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                    CommandParameter="{Binding}" />
        </ContextMenu>
    </Button.ContextMenu>
</Button>

这篇关于WPF命令绑定ItemsControl内的ContextMenu项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆