2013-10-08 93 views
0

我有以下问题。我为菜单和菜单项创建了一个用户控件:wpf mvvm light menu item click eventtocommand

usercontrols启动将viewmodel作为Datacontext和视图模型的构造函数激发并调用模型中的ReylayCommands。当我点击视图中的菜单项时。然后发生事情。我错过了什么?

我的XAML:

<UserControl x:Class="TestDashBoard.Views.MenuItemView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:prop="clr-namespace:TestDashBoard.Properties" 
      xmlns:i="clr namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
      xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4" 
      mc:Ignorable="d" > 
    <Menu IsMainMenu="True"> 
     <MenuItem Header="{x:Static prop:Resources.Setup}"> 
      <MenuItem x:Name="salesSetup" Header="{x:Static prop:Resources.SaleSetup}"> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="Click"> 
         <cmd:EventToCommand Command="{Binding SalesSetupClicked, Mode=OneWay}" /> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
      </MenuItem> 
     </MenuItem> 
    </Menu>  
</UserControl> 

我的视图模型类:

using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Command; 

namespace TestDashBoard.ViewModels 
{ 
    public class MenuItemViewModel : ViewModelBase 
    { 
     public RelayCommand SalesSetupClicked 
     { 
      get; 
      private set; 
     } 
     public RelayCommand InvtSetupClicked 
     { 
      get; 
      private set; 
     } 

     public MenuItemViewModel() 
     { 
      SalesSetupClicked = new RelayCommand(() => 
      { 
       ShowSalesSetup(); 
      }); 

      InvtSetupClicked = new RelayCommand(() => 
      { 
       ShowInvtSetup(); 
      }); 
     } 

     private void ShowSalesSetup() 
     { 
     } 
     private void ShowInvtSetup() 
     { 
     } 
    } 
} 
+0

请问您可以检查这个命令是否可以正常使用Button?如果是,则表示将FocusManager.IsFocusScope =“True”属性设置为Menu。 – Ravuthasamy

+0

您是否在调试模式下检查,如果您的命令正在工作? – Sasha

+0

它工作正常与按钮。我试图使用FocusManger.IsFocusable =“True”。不起作用。要么我把它放在Menu或MenuItem中,我试图点击 – user2063981

回答

0

尝试在你的ViewModel这样做:

使用GalaSoft.MvvmLight;使用GalaSoft.MvvmLight.Command的 ;

namespace TestDashBoard.ViewModels 
{ 
    public class MenuItemViewModel : ViewModelBase 
    { 

     public RelayCommand _salesSetupClicked; 

     public RelayCommand SalesSetupClicked 
     { 
      get 
      { 
       if (_salesSetupClicked == null) 
        _salesSetupClicked = new RelayCommand(ShowSalesSetup); 
       return _salesSetupClicked; 
      }; 
      private set; 
     } 
     public RelayCommand InvtSetupClicked 
     { 
      get; 
      private set; 
     } 

     public MenuItemViewModel() 
     { 
      SalesSetupClicked = new RelayCommand(() => 
      { 
       ShowSalesSetup(); 
      }); 

      InvtSetupClicked = new RelayCommand(() => 
      { 
       ShowInvtSetup(); 
      }); 
     } 

     private void ShowSalesSetup() 
     { 
     } 
     private void ShowInvtSetup() 
     { 
     } 
    } 
} 
+0

不起作用我得到编译错误。试图移动if(_salesSetupClicked == null) _salesSetupClicked = new RelayCommand(ShowSalesSetup);然后它被编译,但没有反应,当我点击菜单项 – user2063981

+0

我找到了我的问题的答案。我将usercontrol链接到主视图,并在usercontrols中拥有菜单控件。 当我将菜单移动到主视图并将RelayCommands移动到主视图模型时。然后它工作。 – user2063981

+0

我很高兴我帮你找到了解决方案! – HappyDump

0

尝试将您想要发生的代码放入构造函数中,但是使用某种方法。

+0

我找到了我的问题的答案。我将usercontrol链接到主视图,并在usercontrols中拥有菜单控件。 当我将菜单移动到主视图并将RelayCommands移动到主视图模型时。然后它工作。 – user2063981

+0

很好,你找到解决方案 – Sasha

相关问题