0

我有MVVM WPF申请。我Menu通过单独的服务创建并设置为加载时间。

我的包含菜单的外壳视图如下:

<ItemsControl x:Name="MainToolbar" cal:RegionManager.RegionName="{x:Static inf:RegionNames.MainToolBarRegion}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1,1,1,1" RenderTransformOrigin="-0.133,-5.917" Height="28" >
    <ItemsControl.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>
            <RotateTransform/>
            <TranslateTransform/>
        </TransformGroup>
    </ItemsControl.RenderTransform>

    <Menu IsMainMenu="True" Margin="0,0,0,0" Height="28" ItemsSource="{Binding Path=Menu}" Width="400">
        <Menu.Resources>
            <Style x:Key="ThemeMenuItemStyle" TargetType="MenuItem" BasedOn="{StaticResource KV_MenuItem}">
                <Setter Property="Header" Value="{Binding Path=Text}"></Setter>
                <Setter Property="Command" Value="{Binding Path=Command}"/>
                <Setter Property="CommandParameter" Value="{Binding Path=Text}"/>
                <Setter Property="IsCheckable" Value="True"/>
                <Setter Property="MinWidth" Value="80"/>
            </Style>
        </Menu.Resources>
    </Menu>
</ItemsControl>

MenuItem后面有一个视图模型:

    public class MenuItemViewModel : NotificationObject
    {

        private string text;
        private ICommand command;
        private IList<MenuItemViewModel> menuItems;

        public MenuItemViewModel()
        {
            this.menuItems = new List<MenuItemViewModel>();
        }

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                if (text != value)
                {
                    text = value;
                    RaisePropertyChanged(() => this.Text);
                }
            }
        }

        public ICommand Command
        {
            get
            {
                return command;
            }
            set
            {
                if (command != value)
                {
                    command = value;
                    RaisePropertyChanged(() => this.Command);
                }
            }
        }

        public IList<MenuItemViewModel> MenuItems
        {
            get { return menuItems; }
            set
            {
                if (menuItems != value)
                {
                    menuItems = value;
                    RaisePropertyChanged(() => this.MenuItems);
                }
            }
        }
    }

我的中继命令:

    public class RelayCommand : ICommand
    {
        #region Fields

        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;

        #endregion // Fields

        #region Constructors

        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion // Constructors

        #region ICommand Members

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        #endregion // ICommand Members
    }

在菜单服务中,我分配命令对象如下:

...
            MenuItemViewModel app = new MenuItemViewModel();
            app.Text = "APP";
            app.Command = new RelayCommand(
                param => this.App(""),
                        param => true);

            MenuItemViewModel exit = new MenuItemViewModel();
            exit.Text = "Exit";
            exit.Command = new RelayCommand(
                        param => this.Exit(""),
                        param => this.CanExit
                        );
            app.MenuItems.Add(exit);
            mService.AddMenu(app);
...

MenuItem使用分配的标题文本加载正常,但是当我单击菜单项时,它不会触发相关的委托方法,例如App("") 或 Exit("")

我做错了什么?

4

0 回答 0