在WPF中,如何从绑定到ListView的集合视图源中的特定项获取命令参数?

在WPF中,如何从绑定到ListView的集合视图源中的特定项获取命令参数?,wpf,listview,command,datatemplate,commandparameter,Wpf,Listview,Command,Datatemplate,Commandparameter,我的目标:右键单击我的ListView中的一个特定项目,弹出一个上下文菜单,选择一个命令,然后我运行一个函数,该函数基于所选项目的上下文菜单 我的ListView的ItemsSource绑定到CollectionViewSource,其源是“Items”的ObservableCollection。 (ListView,binds->CollectionViewSource,source->ObservableCollection类“Item”) 我试图做的是向listview中的所有“项”添加一

我的目标:右键单击我的ListView中的一个特定项目,弹出一个上下文菜单,选择一个命令,然后我运行一个函数,该函数基于所选项目的上下文菜单

我的ListView的ItemsSource绑定到CollectionViewSource,其源是“Items”的ObservableCollection。 (ListView,binds->CollectionViewSource,source->ObservableCollection类“Item”)

我试图做的是向listview中的所有“项”添加一个通用上下文菜单,当为listview中的项选择上下文菜单项时,就会运行一个命令。我可以让命令在一般情况下运行,但我无法获得有关选择上下文菜单的特定项的任何信息/参数

在本例中,“Item”类有一个名为host的字符串,我想将host字符串传递给REQUEST命令,但我无法传递任何CommandParameters

我读过一些关于创建数据模板并使用它的内容,但没有成功。有人能指导我/帮助我吗

以下是一些代码供参考:

列表视图:

<ListView x:Name="ordersList" Margin="0,10,10,0" BorderThickness="2" Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" HorizontalAlignment="Stretch" ItemsSource="{Binding Source={StaticResource cvsOrders}}" SelectionChanged="ordersList_SelectionChanged" SelectedIndex="0" SelectionMode="Extended">
            <ListView.Resources>
                <local:RefundRequestCommand x:Key="refund"></local:RefundRequestCommand>
            </ListView.Resources>
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="test" Command="{StaticResource refund}" CommandParameter="{Binding host}"></MenuItem>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140">
                        <GridViewColumnHeader Name="OrderNumber" Click="sortClick" Tag="orderNumber" Content="Order Number" />
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding orderNumber}" TextAlignment="Center"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
        //On and On.....

实际上,您正在为ListView设置ContextMenu,但您希望将ListViewItem传递到那里。您应该为ListViewItem设置上下文菜单。试试这个

<local:RefundRequestCommand x:Key="refund"/>

    <Style x:Key="MyLVItemStyle" TargetType="ListViewItem">
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu>
                    <MenuItem Header="test" 
                              Command="{StaticResource refund}" 
                              CommandParameter="{Binding host}">
                    </MenuItem>
                </ContextMenu>
            </Setter.Value>
        </Setter>
    </Style>

并使用它,你喜欢的listview

  <ListView x:Name="ordersList" Margin="0,10,10,0" BorderThickness="2" Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" HorizontalAlignment="Stretch" 
              ItemsSource="{Binding Rectangles}" SelectedIndex="0" SelectionMode="Extended" ItemContainerStyle="{StaticResource MyLVItemStyle}">
......

......
此外,您还可以删除ListView中应用的样式,它应该可以正常工作

  <ListView x:Name="ordersList" Margin="0,10,10,0" BorderThickness="2" Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" HorizontalAlignment="Stretch" 
              ItemsSource="{Binding Rectangles}" SelectedIndex="0" SelectionMode="Extended" ItemContainerStyle="{StaticResource MyLVItemStyle}">
......