Quicknote: Binding XAML event to ViewModel

Just a rough note. I was building a WPF interface for a tool i’ve written. I wanted to bind events on this to my ViewModel, instead of have classic “code beside”.

Other than using third party code, the quickest way to do this was to reference the System.Windows.Interactivity.dll. This is part of the Microsoft Expression Blend SDK http://www.microsoft.com/en-us/download/details.aspx?id=3062

I then added a namespace reference as follows: xmlns:i=”http://schemas.microsoft.com/expression/2010/interactivity

I could then bind an even to a property in my ViewModel and thus handle the event there. 

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="IsVisibleChanged">
      <i:InvokeCommandAction Command="{Binding VisibleChanged}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

Related link: http://msdn.microsoft.com/en-us/library/gg405484(v=PandP.40).aspx

You May Also Like

About the Author: rnowik

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.