Get the item doubleclick event of listview

后端 未结 16 1330
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 02:17

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

相关标签:
16条回答
  • 2020-12-01 02:51

    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.

    0 讨论(0)
  • 2020-12-01 02:51

    i see this subject is high on google, there is my simple and working sample :)

    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 is a test Label to see item content :) I add here in this last line a method to Load Page with data from item.

    0 讨论(0)
  • 2020-12-01 02:52

    Here is how to get the selected object and object matching code for the double clicked listview item in a WPF listview:

    /// <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.
        }
    }       
    
    0 讨论(0)
  • 2020-12-01 02:52

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

    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.

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

    0 讨论(0)
提交回复
热议问题