Search code examples
wpfmvvmcode-behindrouted-events

What ways are there for handling a double click event without using codebehind?


I saw an article detailing how to do this using attached behaviors which I like a lot. However, the download doesn't work and the article is pretty old (a couple of years).

What is the current way for handling events like this without using codebehind? Are there any that are MVVM-specific?


Solution

  • You can use the expression blend extensions:

    <ListBox>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <i:InvokeCommandAction Command="CommandNameHere"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>
    

    It is another dependency, but it does seem to work well in .NET 4.

    http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=10801

    Edit: You can also use the CommandParameter To Pass In Extra Things (Like The Selected Item View Model)