中继命令未在菜单项单击时触发
Relay Command not firing on menu item click
我的观点
<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 没有触发
作为一个很好的例子,看看这个 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
。
我的观点
<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 没有触发
作为一个很好的例子,看看这个 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
。