【问题标题】:WPF Command Binding of ContextMenu Item inside ItemsControlItemsControl 内 ContextMenu 项的 WPF 命令绑定
【发布时间】:2017-11-09 13:08:55
【问题描述】:

我的应用程序由 MainWindowContentControl 组成,我根据所选菜单更改 ViewModel。

我作为内容显示的其中一个用户控件包含以下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,后者是DataContextUserControl

如何Command 绑定到ConnectionViewModel,而CommandParameterConnection 对象?

【问题讨论】:

标签: c# wpf contextmenu itemscontrol commandbinding


【解决方案1】:

如果将ButtonTag 属性绑定到ItemsControlDataContext,则可以使用PlacementTargetPlacementTarget 绑定到它:

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

【讨论】:

    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 2021-11-05
    • 2011-04-04
    • 2018-11-20
    • 2016-08-12
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多