Wpf Listview: Attaching a Double-Click (On an Item) Event

WPF ListView: Attaching a double-click (on an item) event

Found the solution from here: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3d0eaa54-09a9-4c51-8677-8e90577e7bac/


XAML:

<UserControl.Resources>
<Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">
<EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" />
</Style>
</UserControl.Resources>

<ListView Name="TrackListView" ItemContainerStyle="{StaticResource itemstyle}">
<ListView.View>
<GridView>
<GridViewColumn Header="Title" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Artist" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Album.Artist.Name}" />
</GridView>
</ListView.View>
</ListView>

C#:

protected void HandleDoubleClick(object sender, MouseButtonEventArgs e)
{
var track = ((ListViewItem) sender).Content as Track; //Casting back to the binded Track
}

DoubleClick Event for ListViewItem in WPF

Not entirely sure what you mean by your last line I need a way to check if what is double clicked is actually a ListViewItem and not empty space., but here are two suggestions:

First, if you want to check if an item is selected in your ListView:

private void contactDblClicked(MouseButtonEventArgs obj)
{
var listView = obj.Source as ListView;
if (listView != null)
{
if (listView.SelectedItem != null)
{
Debug.WriteLine("item selected");
}
else
{
Debug.WriteLine("item not selected");
}
}
}

However, I think you already got that solution and if I understand your question right, you want to check if the user actually clicked an item (and not whitespace) even if an item is selected.

So here is the second approach to check if an item was really clicked:

private void contactDblClicked(MouseButtonEventArgs obj)
{
if (((FrameworkElement) obj.OriginalSource).DataContext is YourRosterItemXType)
{
Debug.WriteLine("item was *really* clicked");
}
}

Where YourRosterItemXType is the type of your binded RosterItemX property. With the above code you check, if the DataContext of the original source of the mouse event is set to YourRosterItemXType. Items in your ListView have that DataContext set and so you check if that mouse event comes really from one of those list items.

Wpf listview item object doubleclick

Try this out...

XAML

<ListView  Name="inbox" BorderThickness="2" Margin="5,0,-5,0" MouseDoubleClick="inbox_OnMouseDoubleClick">

C#

private void inbox_OnMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// Assumes your NewWindow class has a constuctor that takes the Email type.
NewWindow window = new NewWindow((Email)inbox.SelectedItem);
window.Show();
}

ListView with nested ListView double click event fires twice

By the child element you have to listen for the PreviewMouseDoubleClick

<EventSetter Event="PreviewMouseDoubleClick" Handler="ViewBox_MouseDoubleClickChild" />

and in the child event handler add e.Handled = true;:

private void ViewBox_MouseDoubleClickChild(object sender, MouseButtonEventArgs e)
{
var picklb = sender as ListBoxItem;

var pickedsub = picklb.Content as BrowserItem;

MessageBox.Show("Clicked on Child View: " + pickedsub.Name + " Element Id: " + pickedsub.RelatedElement.ToString());

e.Handled = true;
}

This will prevent, that MouseDoubleClick event handler for the parent element will be called.



Related Topics



Leave a reply



Submit