【问题标题】:In WPF, How to get a Command Parameter from a specific item in a Collection View Source that is bound to a ListView?在 WPF 中,如何从绑定到 ListView 的集合视图源中的特定项目中获取命令参数?
【发布时间】:2012-12-30 01:07:51
【问题描述】:

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

我的 ListView 的 ItemsSource 绑定到 CollectionViewSource,它的源是“Items”的 ObservableCollection。 (ListView, binds -> CollectionViewSource, source -> ObservableCollection of class "Item")

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

在本例中,“Item”类有一个名为 host 的字符串,我想将 host 字符串传递给 RefundRequestCommand,但我无法传递任何 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.....

命令:

class RefundRequestCommand : ICommand
{
    TreeViewFilter treeViewFilter;

    public void Execute(object parameter)
    {
        string host = (string)parameter;
        Console.WriteLine(host);  //FOR TESTING
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;
}

【问题讨论】:

    标签: wpf listview command datatemplate commandparameter


    【解决方案1】:

    实际上,您正在为 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 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 中应用的样式,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      相关资源
      最近更新 更多