我的观点
<Button.ContextMenu>
<ContextMenu x:Name="Conn_Context_button" Style="{StaticResource LeftContextMenuStyle}">
<MenuItem Style="{StaticResource LeftContextMenuItemStyle}" Header="{x:Static properties:ResourceWrapper.Dashboard_Connection_Delete}" Click="MenuItem_DeleteConnection_Click" />
<MenuItem Style="{StaticResource LeftContextMenuItemStyle}" Header="{x:Static properties:ResourceWrapper.Dashboard_Connection_Refresh}" Command="{Binding MyViewModel.RefreshCommand}" />
</ContextMenu>
MyViewModel.cs
public RelayCommand RefreshCommand { get; set; }
RefreshCommand = new RelayCommand(RefreshConnection);
private void RefreshConnection(object sender)
{
//My Logic
}
这里,当我点击刷新菜单项
时,RefreshCommand没有触发答案 0 :(得分:0)
作为一个很好的例子,请看一下这个situation。
这是从我目前的一个项目中获取的一段简单代码:
private void PrepareCommands()
{
RefreshCommand = new RelayCommand(RefreshCommandMethod);
AddConfigurationCommand = new RelayCommand(AddConfigurationCommandMethod, param => CanAddConfiguration);
EditConfigurationCommand = new RelayCommand(EditConfigurationCommandMethod, param => CanEditConfiguration);
RemoveConfigurationCommand = new RelayCommand(RemoveConfigurationCommandMethod, param => CanRemoveConfiguration);
}
命令在哪里
#region Commands
public ICommand AddConfigurationCommand { get; set; }
public ICommand EditConfigurationCommand { get; set; }
public ICommand RemoveConfigurationCommand { get; set; }
public ICommand RefreshCommand { get; set; }
#endregion
绑定
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" DockPanel.Dock="Right">
<Button Template="{StaticResource AddButton}" Command="{Binding AddConfigurationCommand}" Margin="3,0" />
<Button Template="{StaticResource EditButton}" Command="{Binding EditConfigurationCommand}" Margin="3,0" />
<Button Template="{StaticResource DeleteButton}" Command="{Binding RemoveConfigurationCommand}" Margin="3,0" />
</StackPanel>
正如Jan Walczak所述,尝试使用ICommand
代替RelayCommand
。如果您已创建自己的RelayCommand
,请不要忘记继承ICommand
。