locked
Adding a Command in a Dynamic MenuItem RRS feed

  • Question

  • Hello, and thank you for taking a look at this question.

    I am developing my own Most Recent Used sub-menu. I have developed my main File Menu items (Open, Close, Save etc) in my XAML and included a binding for these to my commands which are defined in my ViewModel. These all work fine.

    I have decided to save the file path and date into an XML file for simplicity. I can read this in and in my ViewModel I have a ListCollectionView property which exposes the XML elements. Still fine!

    In my XAML I can bind my Recent Items sub-menu to the Filename item in my ListCollectionView without a problem, but I cannot seem to figure out how to bind each sub-menu item to a Command which will determine which file to open.

    My XAML so far is as follows:

    <Menu
                x:Name="MainMenu" Background="#FFFFFFFF" BorderBrush="#FF000000" Margin="0,0,0,2.723" Grid.ColumnSpan="2" d:LayoutOverrides="Height, GridBox">
                <MenuItem Header="_File">
                    <MenuItem Header="_Open" InputGestureText="Ctrl+O" Command="{Binding Path=OpenCommand}"/>
                    <MenuItem Header="_New" InputGestureText="Ctrl+N" Command="{Binding Path=NewCommand}"/>
                    <MenuItem Header="_Close" InputGestureText="Ctrl+Q" Command="{Binding Path=CloseCommand}"/>
                    <MenuItem Header="_Save" InputGestureText="Ctrl+S"/>
                    <Separator/>
    
                    <MenuItem Header="Open _Recent" ItemsSource="{Binding Path=RecentFiles}">
                        <MenuItem.ItemContainerStyle>
                            <Style TargetType="{x:Type MenuItem}">
                                <Setter Property="Header" Value="{Binding Filename}"/>
                                <Setter Property="ItemsSource" Value="{Binding RecenFiles}"/>
                                <Setter Property="Command" Value="{Binding Path=OpenRecentCommand}" />
                            </Style>
                        </MenuItem.ItemContainerStyle>
                    </MenuItem>
                    
                </MenuItem>
                <MenuItem Header="_Help">
                    <MenuItem Header="_About" InputGestureText="Ctrl+A"/>
                </MenuItem>
            </Menu>
    I think at the moment my XAML is trying to bind the Command to an item in my RecentFiles ListCollectionView.

    I would be very grateful for some guidance as to how I can open the file, once selected, using my OpenRecentCommand method in my ViewModel.

    Thank you.
    Wednesday, October 7, 2009 2:42 PM

Answers

  • you can do some thing like below

    <Setter Property="CommandParameter" Value="{Binding Path=Filename}" />

    now you get this parameter to your command and you open file based on parameter.


    Thanks,
    Kiran
    • Marked as answer by Linda Liu Wednesday, October 14, 2009 8:34 AM
    Wednesday, October 7, 2009 3:37 PM

All replies

  • you can do some thing like below

    <Setter Property="CommandParameter" Value="{Binding Path=Filename}" />

    now you get this parameter to your command and you open file based on parameter.


    Thanks,
    Kiran
    • Marked as answer by Linda Liu Wednesday, October 14, 2009 8:34 AM
    Wednesday, October 7, 2009 3:37 PM
  • Thank you very much Kiran,

    I will try this out later today and let you know how I get on.

    Regards
    Wednesday, October 14, 2009 8:36 AM
  • Hi

    I am trying similar stuff with Composite Application Guidance for WPF and Silverlight October 2009.

              <Style x:Key="stDynamicCommandItems" TargetType="{x:Type MenuItem}">
                    <Setter Property="Header" Value="{Binding Path=Header }"/>
                    <Setter Property="Command" Value="{Binding Path=ItemCommand }"/>
                    <Setter Property="CommandParameter" Value="{Binding Path=CommandName}" />
                </Style>

            <MenuItem   
                 ItemsSource="{Binding Path=RecentViewsOfProducts}"
                 ItemContainerStyle="{StaticResource stDynamicCommandItems}">

    Using MVVM and ItemCommand is of type ICommand and using DelegateCommand<T> of Composite App Guidance.
    Getting Null Reference exceptionwhen the item is clicked it the class DelegateCommand<T> of Microsoft.Practices.Composite.Presentation.Commands namespace at bellow bold line:

            bool ICommand.CanExecute(object parameter)
            {
                return CanExecute((T)parameter);
            }




    Is there anything missing? Can someone help please.

    Thanks
    Shailendra

    Wednesday, January 27, 2010 5:06 PM
  • This is not going to work. I had the same issue With the Sub Menus i tried to do this but it's not working ....

     

    for the setter 

     

    <Setter Property="CommandParameter" Value="{Binding Path=Filename}" />

    Or command it's not working.. Any Help 

    Thursday, October 21, 2010 3:43 PM
  • Chris 

     

    Does this resolve your Problem?

    I had the same issue but Command is not working

    Thursday, October 21, 2010 7:46 PM