【发布时间】:2017-11-09 13:08:55
【问题描述】:
我的应用程序由 MainWindow 和 ContentControl 组成,我根据所选菜单更改 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,后者是DataContext 的UserControl。
如何将Command 绑定到ConnectionViewModel,而CommandParameter 是Connection 对象?
【问题讨论】:
-
我相信这里的问题是关于
CommandParameter而不是Command!如果有帮助,您能否查看此链接:stackoverflow.com/questions/22448749/…
标签: c# wpf contextmenu itemscontrol commandbinding