Question

In my View I have a Menu Control which is binded to my ViewModel's Property becauase I want to populate it dynamically.I created a seperated class for my Menu . Here is my My menu class:

 public class MenuItemViewModel : ViewModelBase
 {
    internal MenuItemViewModel()
    {
    }

    private string _menuText;
    public string MenuText
    {
        get { return _menuText; }
        set
        {
            if (_menuText == value)
                return;

            _menuText = value;
            RaisePropertyChanged("MenuText");
        }
    }

    private ObservableCollection<MenuItemViewModel> _children;
    public ObservableCollection<MenuItemViewModel> Children
    {
        get { return _children; }
        set
        {
            _children = value;
            RaisePropertyChanged("Children");
        }
    }

}

and In my MainViewModel I created an Collection property of my MenuItemViewModel here is my MainViewModel:

 public class MainViewModel : ViewModelBase
 {
    public MainViewModel()
    {
        LoadMainMenu();
    }

    #region Menu

    private ObservableCollection<MenuItemViewModel> _topMenuItems;
    public ObservableCollection<MenuItemViewModel> TopMenuItems
    {
        get { return _topMenuItems; }
        set
        {
            if (_topMenuItems == value)
                return;

            _topMenuItems = value; 
            RaisePropertyChanged("TopMenuItems");
        }
    }

    public void LoadMainMenu()
    {
        IList<MenuItemViewModel> fileMenuItems = PopulateFileMenuEntries();
       _topMenuItems.Add(new MenuItemViewModel() { MenuText = "_File", Children = new ObservableCollection<MenuItemViewModel>(fileMenuItems) });
    }

    private IList<MenuItemViewModel> PopulateFileMenuEntries()
    {
        List<MenuItemViewModel> fileMenuItems = new List<MenuItemViewModel>();
        fileMenuItems.Add(new MenuItemViewModel() { MenuText = "Open _Recent" });

        return fileMenuItems;
    }

}

here is my XAML:

 <Window.Resources>
    <WpfApplication3_ViewModel:MainViewModel x:Key="MainViewModelDataSource"
                                             d:IsDataSource="True" />
</Window.Resources>
 <Grid DataContext="{StaticResource MainViewModelDataSource}">
    <Menu
          ItemsSource="{Binding TopMenuItems}"
          Margin="12,0,50,237">
        <Menu.Resources>
            <Style TargetType="{x:Type MenuItem}">
                <Setter Property="Header"
                        Value="{Binding MenuText}" />
                <Setter Property="ItemsSource"
                        Value="{Binding Children}" />

                <Style.Triggers>
                    <DataTrigger Binding="{Binding }"
                                 Value="{x:Null}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Separator Style="{StaticResource {x:Static MenuItem.SeparatorStyleKey}}" />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Menu.Resources>
    </Menu>
</Grid>

When the application run it throw an exception "Exception has been thrown by the target of an invocation."

What is wrong with my code

Was it helpful?

Solution

I have had similar problem and I tracked it down to an uninitialised variable. Your _topMenuItems in your Constructor should be

new ObservableCollection<MenuItemViewModel>()

or

ObservableCollection<MenuItemViewModel> _topMenuItems = new ObservableCollection<MenuItemViewModel>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top