Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:ListViewIndex来自PreviewMouseLeftButtonDown事件,使用命令_Wpf_Listview_Listviewitem - Fatal编程技术网

WPF:ListViewIndex来自PreviewMouseLeftButtonDown事件,使用命令

WPF:ListViewIndex来自PreviewMouseLeftButtonDown事件,使用命令,wpf,listview,listviewitem,Wpf,Listview,Listviewitem,我想使用以下命令从PreviewMouseLeftButtonDown事件中获取ListViewIndex的索引: <ListView Name="ListViewFiles"> <i:Interaction.Triggers> <i:EventTrigger EventName="PreviewMouseLeftButtonDown"> <i:InvokeCommandAction Command="{B

我想使用以下命令从
PreviewMouseLeftButtonDown
事件中获取
ListViewIndex
的索引:

<ListView Name="ListViewFiles">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
            <i:InvokeCommandAction Command="{Binding ListViewItemMouseLeftButtonDownCommand}"
                                   CommandParameter="{Binding ElementName=ListViewFiles, Path=SelectedItem}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListView>

PreviewMouseLeftButtonDown
是在选择项目之前触发的,因此使用
EventTrigger
的方法不起作用

您可以使用
AddHandler
方法和视图代码隐藏中的
handledEventsTo
参数将事件处理程序连接到
MouseLeftButtonDownEvent

ListViewFiles.AddHandler(ListView.MouseLeftButtonDownEvent, new RoutedEventHandler((ss, ee) => 
{
    (DataContext as YourViewModel).ListViewItemMouseLeftButtonDownCommand.Execute(ListViewFiles.SelectedItem);
}), true);
就MVVM而言,这并不比在XAML标记中使用
EventTrigger
更糟糕,但如果您希望能够在多个视图中共享此功能,可以创建:

XAML:

<ListView Name="ListViewFiles" 
    local:MouseLeftButtonDownBehavior.Command="{Binding ListViewItemMouseLeftButtonDownCommand}" />

什么是Window43?它应该替换为“YourViewModel”或定义命令的类的名称。
public static class MouseLeftButtonDownBehavior
{
    public static ICommand GetCommand(ListView listView) =>
        (ICommand)listView.GetValue(CommandProperty);

    public static void SetCommand(ListView listView, ICommand value) =>
        listView.SetValue(CommandProperty, value);

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached(
        "Command",
        typeof(ICommand),
        typeof(MouseLeftButtonDownBehavior),
        new UIPropertyMetadata(null, OnCommandChanged));

    private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ListView listView = (ListView)d;

        ICommand oldCommand = e.OldValue as ICommand;
        if (oldCommand != null)
            listView.RemoveHandler(UIElement.MouseLeftButtonDownEvent, (MouseButtonEventHandler)OnMouseLeftButtonDown);

        ICommand newCommand = e.NewValue as ICommand;
        if (newCommand != null)
            listView.AddHandler(UIElement.MouseLeftButtonDownEvent, (MouseButtonEventHandler)OnMouseLeftButtonDown, true);
    }

    private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        ListView listView = (ListView)sender;
        ICommand command = GetCommand(listView);
        if (command != null)
            command.Execute(listView.SelectedItem);
    }
}
<ListView Name="ListViewFiles" 
    local:MouseLeftButtonDownBehavior.Command="{Binding ListViewItemMouseLeftButtonDownCommand}" />