tags:

views:

314

answers:

2

Hi,

I have a WPF ListView that opens a certain window when double clicked on a certain item inside the list view, but I have a problem. When I double click the GridViewColumn, that also opens up a certain window. Is there a way to detect if the sender is a gridviewColumn or a listView item? Thanks

A: 

in your event handler you typically have two arguments, the first is your sender object, the second is your EventArguments object.

you can check the sender object for the type by using the "is" operator:

private void MyEvent(object sender,EventArgs args )
{
    if ( sender is GridView ) dothis();
}
Muad'Dib
Sorry, but a gridView is different than a GridViewColumn, I am trying to basically do the Headers, not the grid itself. So that won't work
Kevin
somewhere you must have an event handler that opens the window, yes? in said event handler just check the sender to see what type it is.
Muad'Dib
Surely that is just a typo in @Muad's answer? What happens if you have: if (sender as ListViewItem != null) dothis();
slugster
I'm using the IS operator, not the AS operator
Muad'Dib
Yeah, i saw that. I was talking about the type you were comparing the sender to.
slugster
I figured it out, all i did was add a event is mouseEntered in the GridViewColumnHeader and checked if it was in there or not. When it is, then so certain functions. That fixed my problem.
Kevin
A: 

I assume you're handling the MouseDoubleClick event of the ListView ? Instead, you should handle that event on the ListViewItem, not the ListView itself. You can do that easily by setting the event handler in the ListView's ItemContainerStyle :

...
<ListView ...>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <EventSetter Event="MouseDoubleClick" Handler="YourHandler" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
...
Thomas Levesque
I don't think this answers the poster's question, but it answered mine! Thanks!
Elmo Gallen