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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>