简体   繁体   中英

wpf mvvm light menu item click eventtocommand

I have following problem. I have created a usercontrol for menu and menu item:

The usercontrols fires up With the viewmodel as Datacontext and the constructor of the view model fires and invokes the ReylayCommands in the model. When i click on a menuitem in the view. Then nothings happens. I'm missing something?

My 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>

My View Model class:

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

Try to do so in your ViewModel:

using GalaSoft.MvvmLight; using 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()
        {
        }
    }
}

尝试将要发生的代码放入某种构造方法中,而不是放入构造函数中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM