Wpf ListView and ContextMenu with multiple CommandParameters

BigH61 581 Reputation points
2023-01-14T12:25:50.8466667+00:00

I have created striped down test project to simply demonstrate the problem.

I have a series of button with Commands and Multiple CommandParameters to manipulate some images displayed within a ListView. Everything works fine.

I would then like a ContextMenu with MenuItems that duplicate the actions of the buttons but my problem is I cannot get the CommandParameters to work.

Link to test project Test Project

Snippet of xaml the code below:

Thanks for any assistance.


<Window.Resources>
        <ResourceDictionary>
            <vm:MainWindowViewModel x:Key="MainWindowViewModel"/>
            <conv:MultiBindingConverter x:Key="MultiBindingConverter"/>
        </ResourceDictionary>
    </Window.Resources>

    <Window.DataContext>
        <vm:MainWindowViewModel/>
    </Window.DataContext>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="28"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button x:Name="Rotate90Left" Content="Rotate 90 Left" Margin="10,2,10,2" Command="{Binding RotateFlipCommand}">
            <!--#region Option One-->
            <!--<Button.CommandParameter>
                <MultiBinding Converter="{StaticResource MultiBindingConverter}">
                    <Binding ElementName="Rotate90Left" Path="Name"/>
                    <Binding ElementName="MyListView" Path="SelectedIndex"/>
                </MultiBinding>
            </Button.CommandParameter>-->
            <!--#endregion Option One-->

            <!--#region Option Two May then be able to remove ImageItem from ViewModel-->
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource MultiBindingConverter}">
                    <Binding ElementName="Rotate90Left" Path="Name"/>
                    <!-- Preferance would be to simply pass string "Rotate90Left"-->
                    <Binding ElementName="MyListView" Path="SelectedIndex"/>
                    <Binding ElementName="MyListView" Path="SelectedItem"/>
                </MultiBinding>
            </Button.CommandParameter>
            <!--#endregion Option Two-->
        </Button>
        <ListView x:Name="MyListView" Grid.Row="1" ItemsSource="{Binding ImagesCollection}" SelectedIndex="{Binding ImageIndex}" SelectedItem="{Binding ImageItem}" Style="{StaticResource ListBoxStyle}" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding SourceImage}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="150">
                        <Image.ContextMenu>
                        <ContextMenu x:Name="CM">
                                <MenuItem Header="Rotate90Right" Command="{Binding RotateFlipCommand, Source={StaticResource MainWindowViewModel}}">
                                <MenuItem.CommandParameter>
                                    <MultiBinding Converter="{StaticResource MultiBindingConverter}">
                                        <!--This Works but at design time dosnt display correctly-->
                                            <Binding Source="{x:Reference Name=Rotate90Left}" Path="Name"/>
                                        <!--This Works-->
                                            <Binding Path="ImageIndex" Source="{StaticResource MainWindowViewModel}"/>
                                        <!--This passes Null-->
                                            <Binding Path="ImageItem" Source="{StaticResource MainWindowViewModel}"/>
                                        </MultiBinding>
                                </MenuItem.CommandParameter>
                                    <MenuItem.Icon>
                                        <Image Source="/ListViewContextMenu;component/Resources/Rotate90Left.png" />
                                    </MenuItem.Icon>
                                </MenuItem>
                            
                        </ContextMenu>
                        </Image.ContextMenu>
                    </Image>
                </DataTemplate>
            </ListView.ItemTemplate>
            <behaviors:Interaction.Behaviors>
                <bh:ListViewBehavior/>
            </behaviors:Interaction.Behaviors>
        </ListView>
    </Grid>

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,678 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,292 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
767 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,661 Reputation points Microsoft Vendor
    2023-01-16T08:58:14.3666667+00:00

    I update your code as follows, it can run successfully.

     <Window.Resources>
            <ResourceDictionary>
                <vm:MainWindowViewModel x:Key="MainWindowViewModel"/>
                <conv:MultiBindingConverter x:Key="MultiBindingConverter"/>
            </ResourceDictionary>
        </Window.Resources>
    
        <Grid DataContext="{StaticResource MainWindowViewModel}">
            <Grid.RowDefinitions>
                <RowDefinition Height="28"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Button x:Name="Rotate90Left" Content="Rotate 90 Left" Margin="10,2,10,2" Command="{Binding RotateFlipCommand}">
    
    
                <Button.CommandParameter>
                    <MultiBinding Converter="{StaticResource MultiBindingConverter}">
                        <Binding ElementName="Rotate90Left" Path="Name"/>
                        <Binding ElementName="MyListView" Path="SelectedIndex"/>
                        <Binding ElementName="MyListView" Path="SelectedItem"/>
                    </MultiBinding>
                </Button.CommandParameter>
            </Button>
            <ListView x:Name="MyListView" Grid.Row="1" ItemsSource="{Binding ImagesCollection}"
                      SelectedIndex="{Binding ImageIndex}" 
                      SelectedItem="{Binding ImageItem}" Style="{StaticResource ListBoxStyle}" >
                <ListView.ContextMenu >
                    <ContextMenu x:Name="CM">
                      
                        <MenuItem Header="Rotate90Right" Command="{Binding RotateFlipCommand, Source={StaticResource MainWindowViewModel}}">
                            <MenuItem.CommandParameter>
                                <MultiBinding Converter="{StaticResource MultiBindingConverter}">
                                   
                                    <Binding Source="{x:Reference Name=Rotate90Left}" Path="Name"/>
                                 
                                    <Binding Path="ImageIndex" Source="{StaticResource MainWindowViewModel}"/>
                                  
                                    <Binding Path="ImageItem" Source="{StaticResource MainWindowViewModel}"/>
                                </MultiBinding>
                            </MenuItem.CommandParameter>
                            <MenuItem.Icon>
                                <Image Source="/ListViewContextMenu;component/Resources/Rotate90Left.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                     
    
                    </ContextMenu>
                </ListView.ContextMenu>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding SourceImage}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="150">
                          
                        </Image>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <behaviors:Interaction.Behaviors>
                    <bh:ListViewBehavior/>
                </behaviors:Interaction.Behaviors>
            </ListView>
        </Grid>
    
    

    The result: enter image description here


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful