tags:

views:

3930

answers:

3

I have an Project object it has a Name Property.

The ViewModel for project contains a ICommand called SaveCommand which persists in the database.

I have a textbox in my UI that binds to Name

<TextBox Text="{Binding Name}" LostFocus="??????" />

How do I bind my SaveCommand to the TextBox ecent LostFocus? So when the textbox loses focus the ICommand SaveCommand will run?

Please ignore this question. I solved it by adding a new property to my modelView.

+1  A: 

You can only bind command to a Command property.

BUT, there is two solutions:

-Fire your event on a method which call a command in your VM (but some code will be not testable)

-you can develop your own MarkupExtension to do LostFocus="{YouExtension YouCommand}"

Nicolas Dorier
A: 

Hi. Try looking into using attached behaviours. There is a lot of articles around about this, so just try to google for it. Here is one:

link text

Best regards, Rune

Rune Kvamme
Looking in to Attached Behavours.
Emil C
Aha. Had that question too. Will look into attached behaviors. Here is a link on Codeproject: http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx
Tom Deleu
+1  A: 

Caliburn (http://www.codeplex.com/caliburn) has a reusable "Actions" concept which uses attached behaviors to generically allow you to bind events to methods on your ViewModel.

wekempf