简体   繁体   中英

Implementing double-click event for ListView

I've seen plenty of examples of how to add a double-click event for each ListView item, but I can't figure out how to implement it to my code. The best example I found so far is this one in MS docs, but it still doesn't help me handle this :

<ListView Name="listViewItem" ClipToBounds="True" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" SizeChanged="ListView_SizeChanged" TextOptions.TextHintingMode="Animated" Margin="0,0,117,0">
            <ListView.View>
                <GridView AllowsColumnReorder="False">
                    <GridViewColumn Header="Task ID" Width="0">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding TaskID}" TextWrapping="Wrap" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Tast Title" Width="150">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding TaskTitle}" TextWrapping="Wrap" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Task Deadline" Width="275">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding TaskDeadline}" TextWrapping="Wrap" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Task Group" Width="150">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding TaskGroup}" TextWrapping="Wrap" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Task Contact" Width="200">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding TaskContact}" TextWrapping="Wrap" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Task Workers" Width="200">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding TaskWorkers}" TextWrapping="Wrap" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

I have a helpers class that sets values on load event ->

public void GetGridTasks(ref ListView listViewItem)
        {
            SqlCommand sqlCommandRefresh = new SqlCommand("", dataconnection);

            sqlCommandRefresh.CommandText = "SELECT TaskID, TaskTitle, TaskDeadline, TaskGroup, TaskContact, TaskWorkers FROM Tasks";

            SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommandRefresh);
            DataTable dt = new DataTable("Tasks");
            sqlAdapter.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                dr["TaskWorkers"] = dr["TaskWorkers"].ToString().Replace("||", ", ");
            }

            listViewItem.ItemsSource = dt.DefaultView;
        }

This is basically TextBlocks inside GridView inside ListView. Can't really wrap my head around fixing this mess.

Just use the MouseDoubleClick Event from the ListView.

Example:

<ListView MouseDoubleClick="ListView_MouseDoubleClick">
//Items here
</ListView>

In your code behind you simply add an event handler for that

void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e){
    var item = ((FrameworkElement) e.OriginalSource).DataContext
    var myItem = item as *CastToWhateverTypeYouNeed*
    if (item != null){
        //Here you have your item
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM