Double Click a ListBox item to open a browser

Sebastian Müller picture Sebastian Müller · May 4, 2009 · Viewed 37.5k times · Source

I have a ListBox in my wpf window that binds to an ObervableCollection. I want to open the browser if someone clicks on an element of the ListBox (just like a link). Can someone tell me how to do this? I found something with listboxviews, does it only work this way or is there a way by just using the ListBox?

Yours

Sebastian

Answer

Bob King picture Bob King · May 4, 2009

You can add a style to ListBox.ItemContainerStyle, and add an EventSetter there:

<ListBox>
    ....
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

ListBoxItem_MouseDoubleClick is a method in your code behind with the correct signature for MouseDoubleClick.