C# ItemsControl内ContextMenu项的WPF命令绑定
我的应用程序由一个C# ItemsControl内ContextMenu项的WPF命令绑定,c#,wpf,contextmenu,itemscontrol,commandbinding,C#,Wpf,Contextmenu,Itemscontrol,Commandbinding,我的应用程序由一个main窗口和一个ContentControl组成,我根据所选菜单更改ViewModel 我显示为内容的其中一个用户控件包含以下WrapPanel: <UserControl ...> <Grid> <WrapPanel> <ItemsControl ItemsSource="{Binding Connections}"> <ItemsContr
main窗口
和一个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>
上下文菜单上的命令
不起作用,因为它试图调用连接
对象上的连接移除命令
,而不是连接视图模型
,后者是用户控件的数据上下文
如何将命令
绑定到连接视图模型
,而命令参数
是连接
对象?如果将按钮
的标记
属性绑定到项控件
的数据上下文
,然后,您可以使用上下文菜单的PlacementTarget
绑定到它:
<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>
如果将按钮的标记
属性绑定到项控件
的数据上下文
,则可以使用上下文菜单
的放置目标
绑定到它:
<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>
我相信这里的问题是关于CommandParameter
而不是Command
!如果有帮助的话,你能检查一下这个链接吗:。我相信这里的问题是关于CommandParameter
而不是Command
!你能检查一下这个链接吗。