1

In my WPF MVVM (using Prism) application I have a listview. In this listview I have bound a Ctrl-C key input to a copy function with an IList parameter. (see CopyChannelChangeNotificationToClipboardCommand) This works well.

Now I want to add a context menu that has a MenuItem with "Copy Ctrl-C" functionality. What I do not understand is how to bind the CommandParameter from the MenuItem so that it provides the ListView's Selected items? The idea is to use the same function as the input bound key, i.e. get the IList from the listview to be attached as a command parameter. I've tried to read quite a few posts here, tried even more but all result in the objList parameter to be null. If anyone has suggestions on how to accomplish this I'd be grateful.

My view (partial)


<ListView Grid.Row="1" ItemTemplate="{StaticResource ChannelChangeDataTemplateKey}" ItemsSource="{Binding ChannelChangeNotifications}" lb:ListBoxBehavior.ScrollOnNewItem="true">
    <ListView.InputBindings>
        <KeyBinding Key="C" Modifiers="Control" Command="{Binding CopyChannelChangeNotificationToClipboardCommand}" CommandParameter="{Binding Path=SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>
    </ListView.InputBindings>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ContextMenu>
        <ContextMenu>
            <MenuItem Header="{x:Static p:Resources.UICopySelectedItems}" Command="{Binding CopySelectedItemsClickCommand}" CommandParameter="{Binding Path=SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"></MenuItem>
        </ContextMenu>
    </ListView.ContextMenu>
</ListView>

and my view model code


public Prism.Commands.DelegateCommand<IList> CopyChannelChangeNotificationToClipboardCommand => new Prism.Commands.DelegateCommand<IList>(CopyChannelChangeNotificationToClipboard);
public Prism.Commands.DelegateCommand<IList> CopySelectedItemsClickCommand => new Prism.Commands.DelegateCommand<IList>(CopyChannelChangeNotificationToClipboard);

private void CopyChannelChangeNotificationToClipboard(IList objList)
{
  if (objList != null)
  {
    // Copy selected list view objects to clipboard
    // Works well when coming from CopyChannelChangeNotificationToClipboardCommand
    // but not when coming from CopySelectedItemsClickCommand
    // since objList is always null
  }
}

metscore
  • 79
  • 7
  • Possible duplicate of [ListBox.ContextMenu Get Selected Item via Command](https://stackoverflow.com/questions/33548871/listbox-contextmenu-get-selected-item-via-command) – dymanoid Sep 06 '19 at 14:59
  • @dymanoid Thanks. I obviously missed these for some reason. I'll take a look at them ! – metscore Sep 09 '19 at 06:44
  • @dymanoid I looked at the other posts and yes, they solved my problem. Thanks again for you effort ! King regards Metscore – metscore Sep 09 '19 at 07:09

0 Answers0