Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Add a command to auto-generated by binding menu items

MVVM is used. I created separate menu 'Recent files' which gets its items from binding. It looks like that:

enter image description here

        <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
        </MenuItem>

Now, I would like to add Command to each of the those auto-generated items, which should get the path as command parameter and execute import file action by click.

Could you please suggest how can it be done in MVVM way?

like image 391
trickbz Avatar asked Dec 31 '12 07:12

trickbz


People also ask

What is a binding command?

bind command is Bash shell builtin command. It is used to set Readline key bindings and variables. The keybindings are the keyboard actions that are bound to a function. So it can be used to change how the bash will react to keys or combinations of keys, being pressed on the keyboard.

What is the use of ICommand in WPF?

Use of ICommand Since UpdateCommand is nothing but an ICommand instance, while loading the window it will check the CanExecute return value and if it returns true then it will enable the button control and the Execute method is ready to be used otherwise the button control is disabled.


1 Answers

Again, found the solution by myself. I tried to put the command in wrong way like below, and it doesn't work:

            <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Command" Value="{Binding ImportRecentItemCommand}" />
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>

Here is the right approach. Still don't understand how it works, have to learn WPF deeply!

            <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>

EDIT: The final version

XAML:

            <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
                    <Setter Property="CommandParameter" Value="{Binding}" />
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>

ViewModel: MVVM Light Toolkit is used, RelayCommand goes from there:

        private ICommand _importRecentItemCommand;

        public ICommand ImportRecentItemCommand
        {
            get { return _importRecentItemCommand ?? (_importRecentItemCommand = new RelayCommand<object>(ImportRecentItemCommandExecuted)); }
        }

        private void ImportRecentItemCommandExecuted(object parameter)
        {
            MessageBox.Show(parameter.ToString());
        }

Enjoy

like image 184
trickbz Avatar answered Oct 30 '22 01:10

trickbz