C# 获取listview的item双击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/929179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:25:25  来源:igfitidea点击:

Get the item doubleclick event of listview

c#.netwpfwpf-controls

提问by Sauron

What do I need to do in order to reference the double click event for a listview control?

为了引用列表视图控件的双击事件,我需要做什么?

采纳答案by Oskar

I'm using something like this to only trigger on ListViewItem double-click and not for example when you double-click on the header of the ListView.

我使用这样的东西只在 ListViewItem 双击时触发,而不是例如当你双击 ListView 的标题时。

private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DependencyObject obj = (DependencyObject)e.OriginalSource;

    while (obj != null && obj != myListView)
    {
        if (obj.GetType() == typeof(ListViewItem))
        {
            // Do something here
            MessageBox.Show("A ListViewItem was double clicked!");

            break;
        }
        obj = VisualTreeHelper.GetParent(obj);
    }
}

回答by Sauron

In the ListBox DoubleClick event get the selecteditem(s) member of the listbox, and there you are.

在 ListBox DoubleClick 事件中,获取列表框的 selecteditem(s) 成员,然后就可以了。

void ListBox1DoubleClick(object sender, EventArgs e)
    {
        MessageBox.Show(string.Format("SelectedItem:\n{0}",listBox1.SelectedItem.ToString()));
    }

回答by Joel

Either use the MouseDoubleClick event, and also, all the MouseClick events have a click count in the eventargs variable 'e'. So if e.ClickCount == 2, then doubleclicked.

要么使用 MouseDoubleClick 事件,另外,所有 MouseClick 事件在 eventargs 变量“e”中都有一个点击计数。所以如果 e.ClickCount == 2,则双击。

回答by Ana Betts

It's annoying, but the best way to do it is something like:

这很烦人,但最好的方法是:

<DataTemplate Name="MyCoolDataTemplate">
    <Grid Loaded="HookLVIClicked" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">
        <!-- your code here -->
    </Grid>
</DataTemplate>

Then in the code:

然后在代码中:

public void HookLVIClicked(object sender, RoutedEventArgs e) {
    var fe = (FrameworkElement)sender;
    var lvi = (ListViewItem)fe.Tag;
    lvi.MouseDoubleClick += MyMouseDoubleClickHandler;
} 

回答by Oliver

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" />
    </Style>
</ListView.ItemContainerStyle>

The only difficulty then is if you are interested in the underlying object the listviewitem maps to e.g.

唯一的困难是如果您对 listviewitem 映射到的底层对象感兴趣,例如

private void listViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListViewItem item = sender as ListViewItem;
    object obj = item.Content;
}

回答by AngeDeLaMort

I needed that as well. I found that on msdn:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.activation.aspx

I think this delegate is for that.

我也需要那个。我在 msdn 上发现了这一点:

http: //msdn.microsoft.com/en-us/library/system.windows.forms.listview.activation.aspx

我认为这个代表就是为了这个。

回答by Dmitry G.

I found this on Microsoft Dev Center. It works correctly and ignores double-clicking in wrong places. As you see, the point is that an item gets selected before double-click event is triggered.

我在 Microsoft Dev Center 上找到了这个。它工作正常并忽略在错误的地方双击。如您所见,关键是在触发双击事件之前选择了一个项目。

private void listView1_DoubleClick(object sender, EventArgs e)
{
    // user clicked an item of listview control
    if (listView1.SelectedItems.Count == 1)
    {
        //do what you need to do here            
    }
}

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/588b1053-8a8f-44ab-8b44-2e42062fb663

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/588b1053-8a8f-44ab-8b44-2e42062fb663

回答by JasonEdinburgh

    private void positionsListView_DoubleClick(object sender, EventArgs e)
    {
        if (positionsListView.SelectedItems.Count == 1)
        {
            ListView.SelectedListViewItemCollection items = positionsListView.SelectedItems;

            ListViewItem lvItem = items[0];
            string what = lvItem.Text;

        }
    }

回答by Noctis

Was having a similar issue with a ListBox wanting to open a window (Different View) with the SelectedItem as the context (in my case, so I can edit it).

ListBox 有一个类似的问题,想要打开一个以 SelectedItem 作为上下文的窗口(不同视图)(在我的情况下,所以我可以编辑它)。

The three options I've found are: 1. Code Behind 2. Using Attached Behaviors 3. Using Blend's i:Interaction and EventToCommand using MVVM-Light.

我发现的三个选项是: 1. 代码隐藏 2. 使用附加行为 3. 使用 Blend 的 i:Interaction 和 EventToCommand 使用 MVVM-Light。

I went with the 3rd option, and it looks something along these lines:

我选择了第三个选项,它看起来像这样:

<ListBox x:Name="You_Need_This_Name"  
ItemsSource="{Binding Your_Collection_Name_Here}"
SelectedItem="{Binding Your_Property_Name_Here, UpdateSourceTrigger=PropertyChanged}"
... rest of your needed stuff here ...
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
    <Command:EventToCommand Command="{Binding Your_Command_Name_Here}" 
        CommandParameter="{Binding ElementName=You_Need_This_Name,Path=SelectedItem}"     />
    </i:EventTrigger>
</i:Interaction.Triggers>

That's about it ... when you double click on the item you want, your method on the ViewModel will be called with the SelectedItem as parameter, and you can do whatever you want there :)

就是这样......当你双击你想要的项目时,你在 ViewModel 上的方法将使用 SelectedItem 作为参数被调用,你可以在那里做任何你想做的事情:)

回答by CrimsonFantasy

for me, I do double click of ListView in this code section .

对我来说,我在这个代码部分双击 ListView 。

    this.listView.Activation = ItemActivation.TwoClick;

    this.listView.ItemActivate += ListView1_ItemActivate;

ItemActivate specify how user activate with items

ItemActivate 指定用户如何激活项目

When user do double click, ListView1_ItemActivate will be trigger. Property of ListView ItemActivate refers to access the collection of items selected.

当用户双击时,将触发 ListView1_ItemActivate。ListView ItemActivate 的属性是指访问所选项目的集合。

    private void ListView1_ItemActivate(Object sender, EventArgs e)
    {

        foreach (ListViewItem item in listView.SelectedItems)
           //do something

    }

it works for me.

这个对我有用。