wpf mvvm light菜单项单击eventtocommand

时间:2013-10-08 10:26:22

标签: c# wpf mvvm

我有以下问题。我为菜单和菜单项创建了一个usercontrol:

启动usercontrols将viewmodel作为Datacontext,视图模型的构造函数触发并调用模型中的ReylayCommands。当我点击视图中的menuitem时。然后没有发生。我错过了什么?

我的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()
        {
        }
    }
}

2 个答案:

答案 0 :(得分: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()
        {
        }
    }
}

答案 1 :(得分:0)

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