2016-08-10 4 views
1

My Viewреле Команда не стреляя по пункту меню нажмите

<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

Возможно, вместо этого использовать ICommand? – Whencesoever

+0

Возможно, datacontext неверен, проверьте на наличие ошибок привязки на выходе. Если 'MyViewModel' является datacontext,' Command = "{Binding RefreshCommand}' будет решать это. – Natxo

+2

Это не так просто: попробуйте http://stackoverflow.com/questions/9994241/mvvm-binding-command-to- contextmenu-item –

ответ

0

В качестве хорошего примера взглянем на это situation.

Вот простой фрагмент кода, взятый из одного из моих текущих Projets:

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> 

Как было сказано выше Ян Walczak , попробуйте использовать ICommand вместо RelayCommand. Если вы создали свой собственный RelayCommand, не забудьте наследовать от ICommand.

Смежные вопросы