获取listview的项目双击事件

问题描述 投票:34回答:16

为了引用listview控件的双击事件,我需要做什么?

c# .net wpf wpf-controls
16个回答
36
投票

我正在使用这样的东西只触发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);
    }
}

1
投票

您可以先获取ListView,然后获取Selected ListViewItem。我有一个ListBox的例子,但ListView应该是类似的。

private void listBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ListBox box = sender as ListBox;
        if (box == null) {
            return;
        }
        MyInfo info = box.SelectedItem as MyInfo;
        if (info == null)
            return;
        /* your code here */
        }
        e.Handled = true;
    }

0
投票

使用MouseDoubleClick事件,并且所有MouseClick事件在eventargs变量'e'中都有一个点击计数。因此,如果e.ClickCount == 2,则双击。


0
投票

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

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

0
投票

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

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

然后在代码中:

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

0
投票

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

我找到的三个选项是:1。代码背后2.使用附加行为3.使用Blend的i:使用MVVM-Light进行交互和EventToCommand。

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

<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>

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


0
投票

发件人的类型为ListView而不是ListViewItem。

    private void listViewTriggers_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        ListView triggerView = sender as ListView;
        if (triggerView != null)
        {
            btnEditTrigger_Click(null, null);
        }
    }

-1
投票

我看到这个主题在谷歌上很高,有我的简单和工作样本:)

XAML:

    <ListView Name="MainTCList" HorizontalAlignment="Stretch" MinHeight="440" Height="Auto" Margin="10,10,5.115,4" VerticalAlignment="Stretch" MinWidth="500" Width="Auto" Grid.Column="0" MouseDoubleClick="MainTCList_MouseDoubleClick" IsSynchronizedWithCurrentItem="True">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="UserTID" DisplayMemberBinding="{Binding UserTID}" Width="80"/>
                <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" Width="410" />
            </GridView>
        </ListView.View>
    </ListView>

C#

    private void MainTCList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
            TC item = (TC)MainTCList.Items.CurrentItem;
            Wyswietlacz.Content = item.UserTID;  
    }

Wyswietlacz是一个测试标签来查看项目内容:)我在这最后一行添加了一个方法来加载页面中包含来自项目的数据。


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

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

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

8
投票
    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;

        }
    }

7
投票

使用ListView.HitTest方法

    private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        var senderList  = (ListView) sender;
        var clickedItem = senderList.HitTest(e.Location).Item;
        if (clickedItem != null)
        {
            //do something
        }            
    }

或旧的方式

    private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        var senderList  = (ListView) sender;                        
        if (senderList.SelectedItems.Count == 1 && IsInBound(e.Location, senderList.SelectedItems[0].Bounds))
        {
            //Do something
        }
    }

    public  bool IsInBound(Point location, Rectangle bound)
    {
        return (bound.Y <= location.Y && 
                bound.Y + bound.Height >= location.Y &&
                bound.X <= location.X && 
                bound.X + bound.Width >= location.X);
    }

5
投票

我也需要它。我发现在msdn上: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.activation.aspx 我认为这个代表就是为了这个。


4
投票

我还没有足够大的声誉分数来添加评论,这将是最有帮助的,但这与那些询问.Net 4.5解决方案的人有关。

您可以使用鼠标X和Y坐标以及ListView方法GetItemAt来查找已单击的项目。

private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListViewItem item = myListView.GetItemAt(e.X, e.Y)
    // Do something here
}

3
投票

对我来说,我在这段代码中双击ListView。

    this.listView.Activation = ItemActivation.TwoClick;

    this.listView.ItemActivate += ListView1_ItemActivate;

ItemActivate specify how user activate with items

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

    private void ListView1_ItemActivate(Object sender, EventArgs e)
    {

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

    }

这个对我有用。


1
投票

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

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


1
投票

以下是如何在WPF列表视图中获取双击列表视图项的选定对象和对象匹配代码:

/// <summary>
/// Get the object from the selected listview item.
/// </summary>
/// <param name="LV"></param>
/// <param name="originalSource"></param>
/// <returns></returns>
private object GetListViewItemObject(ListView LV, object originalSource)
{
    DependencyObject dep = (DependencyObject)originalSource;
    while ((dep != null) && !(dep.GetType() == typeof(ListViewItem)))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }
    if (dep == null)
        return null;
    object obj = (Object)LV.ItemContainerGenerator.ItemFromContainer(dep);
    return obj;
}

private void lvFiles_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    object obj = GetListViewItemObject(lvFiles, e.OriginalSource);
    if (obj.GetType() == typeof(MyObject))
    {
        MyObject MyObject = (MyObject)obj;
        // Add the rest of your logic here.
    }
}       
© www.soinside.com 2019 - 2024. All rights reserved.