views:

345

answers:

0

If you use the DelegateCommand in the MVVM Template and build menus dynamically as in the code below, then the DelegateCommand will not fire when the user clicks on the icon area on the MenuItem.

Is there anyway to either fix this or make the icon area (to the left of the header) disappear?

MainView.xaml:

<Window x:Class="TestMenuMvvm.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:TestMenuMvvm.Commands"
    xmlns:vm="clr-namespace:TestMenuMvvm.ViewModels"
    xmlns:v="clr-namespace:TestMenuMvvm.Views"
    Title="Main Window" Height="400" Width="800">

    <Window.Resources>
        <DataTemplate x:Key="MenuTemplate">
            <MenuItem
                Header="{Binding Title}" 
                Command="{Binding DataContext.SwitchPageCommand,
                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}" 
                CommandParameter="{Binding IdCode}"/>
        </DataTemplate>

    </Window.Resources>

    <DockPanel LastChildFill="False">
        <Menu DockPanel.Dock="Top">
            <MenuItem 
                Header="File" ItemsSource="{Binding AllPageViewModels}"
                      ItemTemplate="{StaticResource MenuTemplate}"/>
        </Menu>

    </DockPanel>

</Window>

MainViewModel.cs:

using System;
using System.Windows;
using System.Windows.Input;
using TestMenuMvvm.Commands;
using System.Collections.ObjectModel;

namespace TestMenuMvvm.ViewModels
{
    public class MainViewModel : ViewModelBase
    {

        #region DelegateCommand: SwitchPage
        private DelegateCommand switchPageCommand;

        public ICommand SwitchPageCommand
        {
            get
            {
                if (switchPageCommand == null)
                {
                    switchPageCommand = new DelegateCommand(SwitchPage, CanSwitchPage);
                }
                return switchPageCommand;
            }
        }

        private void SwitchPage()
        {
            Console.WriteLine("was in switch page");
        }

        private bool CanSwitchPage()
        {
            return true;
        }
        #endregion


        #region ViewModelProperty: AllPageViewModels
        private ObservableCollection<ViewModelBase> _allPageViewModels = new ObservableCollection<ViewModelBase>();
        public ObservableCollection<ViewModelBase> AllPageViewModels
        {
            get
            {
                return _allPageViewModels;
            }

            set
            {
                _allPageViewModels = value;
                OnPropertyChanged("AllPageViewModels");
            }
        }
        #endregion

        public MainViewModel()
        {
            PageCloseViewModel pageCloseViewModel = new PageCloseViewModel();
            PageOpenViewModel pageOpenViewModel = new PageOpenViewModel();

            _allPageViewModels.Add(pageCloseViewModel);
            _allPageViewModels.Add(pageOpenViewModel);
        }

    }
}

PageCloseViewModel.cs:

namespace TestMenuMvvm.ViewModels
{
    class PageCloseViewModel : ViewModelBase
    {

        #region ViewModelProperty: Title
        private string _title;
        public string Title
        {
            get
            {
                return _title;
            }

            set
            {
                _title = value;
                OnPropertyChanged("Title");
            }
        }
        #endregion

        public PageCloseViewModel()
        {
            Title = "Close";
        }
    }
}

PageOpenViewModel.cs:

namespace TestMenuMvvm.ViewModels
{
    class PageOpenViewModel : ViewModelBase
    {
        #region ViewModelProperty: Title
        private string _title;
        public string Title
        {
            get
            {
                return _title;
            }

            set
            {
                _title = value;
                OnPropertyChanged("Title");
            }
        }
        #endregion

        public PageOpenViewModel()
        {
            Title = "Open";
        }
    }
}