2013-10-08 92 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

相關問題