EventToCommand behavior (MVVM Light Toolkit V3 alpha 2)

98 views
Skip to first unread message

Laurent Bugnion, GalaSoft

unread,
Nov 5, 2009, 5:30:06 PM11/5/09
to wpf-di...@googlegroups.com

Hey guys,

 

You might have seen it already, but I just posted a new alpha of the MVVM Light Toolkit. Featured in this release is a new behavior named EventToCommand. This behavior can be added to any FrameworkElement (including panels, shapes, etc…) and can bind any event of this element to an ICommand (typically on the ViewModel). Since it is a behavior, you have all the benefit of rich Blend support, but adding it in XAML is not that hard either.

 

It is inspired of the Blend team’s InvokeDataCommand (with the Blend team’s blessings ;)), but with a few additions:

 

·         You can data bind the CommandParameter (also in Silverlight) or set a hard coded value if you prefer.

·         You can choose (opt-in) to have the attached element be automatically disabled when CanExecute returns false. This is dynamic, i.e. it reacts to ICommand.CanExecuteChanged. This is an opt-in, because since you can have multiple EventToCommand on one single element, you probably don’t want them to fight to set the state of the control.

o   In Silverlight, this works only on Controls, because other elements don’t have IsEnabled.

o   In WPF, this works for any FrameworkElement, so you can disable/enable a panel, for example, and use Style.Triggers to modify the visual appearance.

 

For this component, I create an “Extras” DLL in order to keep the “essentials” lightweight. The Extras is optional, and will contain added value components such as this one. It does also require System.Windows.Interactivity.

 

More info here:

http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx

 

There is a sample application too. Note that the code is alpha, so changes to the API are possible, but it is well tested, so most bugs should be out. I’d love feedback if you have some.

 

Cheers,

Laurent

--

Laurent Bugnion [Microsoft MVP, MCP]

Blog: http://blog.galasoft.ch

Web: http://www.galasoft.ch

Support children in Calcutta: http://www.calcutta-espoir.ch

 

cid:image001.png@01C9C8AA.B722DA80

My

business

card

as

Microsoft Tag

 

 

image001.png

Peter O'Hanlon

unread,
Nov 6, 2009, 4:10:22 AM11/6/09
to wpf-di...@googlegroups.com
Very nice mate - a nice clean implementation.
--
Peter O'Hanlon
image001.png

Daniel Vaughan

unread,
Nov 6, 2009, 5:41:30 AM11/6/09
to WPF Disciples
Very nice release Laurent.

Have you considered implementing some routed command infrastructure?
Your post prompted me to see what's out there and I found Andrew
Whiddett's RoutedEvent implementation via Josh's blog.
http://sl3routedevents.codeplex.com/

Cheers,
Daniel


On Nov 5, 11:30 pm, "Laurent Bugnion, GalaSoft" <laur...@galasoft.ch>
wrote:
> Hey guys,
>
> You might have seen it already, but I just posted a new alpha of the MVVM
> Light Toolkit. Featured in this release is a new behavior named
> EventToCommand. This behavior can be added to any FrameworkElement
> (including panels, shapes, etc.) and can bind any event of this element to
> an ICommand (typically on the ViewModel). Since it is a behavior, you have
> all the benefit of rich Blend support, but adding it in XAML is not that
> hard either.
>
> It is inspired of the Blend team's InvokeDataCommand (with the Blend team's
> blessings ;)), but with a few additions:
>
> .         You can data bind the CommandParameter (also in Silverlight) or
> set a hard coded value if you prefer.
>
> .         You can choose (opt-in) to have the attached element be
> automatically disabled when CanExecute returns false. This is dynamic, i.e.
> it reacts to ICommand.CanExecuteChanged. This is an opt-in, because since
> you can have multiple EventToCommand on one single element, you probably
> don't want them to fight to set the state of the control.
>
> o   In Silverlight, this works only on Controls, because other elements
> don't have IsEnabled.
>
> o   In WPF, this works for any FrameworkElement, so you can disable/enable a
> panel, for example, and use Style.Triggers to modify the visual appearance.
>
> For this component, I create an "Extras" DLL in order to keep the
> "essentials" lightweight. The Extras is optional, and will contain added
> value components such as this one. It does also require
> System.Windows.Interactivity.
>
> More info here:
>
> http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alph...
> nttocommand-behavior.aspx
>
> There is a sample application too. Note that the code is alpha, so changes
> to the API are possible, but it is well tested, so most bugs should be out.
> I'd love feedback if you have some.
>
> Cheers,
>
> Laurent
>
> --
>
> Laurent Bugnion [Microsoft MVP, MCP]
>
> Blog:  <http://www.galasoft.ch/>http://blog.galasoft.ch
>
> Web:  <http://www.galasoft.ch/>http://www.galasoft.ch
>
> Support children in Calcutta:  <http://www.calcutta-espoir.ch/>http://www.calcutta-espoir.ch
>
> cid:image001....@01C9C8AA.B722DA80
>
>  <http://galasoft.ch/contact/mobile/> My
>
>  <http://galasoft.ch/contact/mobile/> business
>
>  <http://galasoft.ch/contact/mobile/> card
>
> as
>
>  <http://www.microsoft.com/tag> Microsoft Tag
>
>  image001.png
> 7KViewDownload

Sacha Barber

unread,
Nov 6, 2009, 8:15:36 AM11/6/09
to wpf-di...@googlegroups.com
Obviously Marlon and I and Mark smith already have this for WPF in our various frameworks, but its nice to have for SL
--
Sacha Barber
sacha....@gmail.com
image001.png

Laurent Bugnion

unread,
Nov 6, 2009, 9:00:36 AM11/6/09
to WPF Disciples
Hey Sacha,

Isn't Mark's and your implementation attached behavior, not Blend
behaviors? Since my toolkit caters primarily to people who use Blend a
lot, I chose to start from the Expression Blend sample implementation.
I also checked Rishi's implementation (in nRoute) and chatted with him
a few times. I love attached behaviors, but it felt more natural to
implement a Blend behavior for that particular functionality. It has
been well received by my users so far, actually.

Correct me if I am wrong though.

Cheers,
Laurent

On Nov 6, 2:15 pm, Sacha Barber <sacha.bar...@gmail.com> wrote:
> Obviously Marlon and I and Mark smith already have this for WPF in our
> various frameworks, but its nice to have for SL
>
>  image001.png
> 7KViewDownload
> >>http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alph...
>
> >> There is a sample application too. Note that the code is alpha, so changes
> >> to the API are possible, but it is well tested, so most bugs should be out.
> >> I’d love feedback if you have some.
>
> >> Cheers,
>
> >> Laurent
>
> >> --
>
> >> Laurent Bugnion [Microsoft MVP, MCP]
>
> >> Blog:http://blog.galasoft.ch<http://www.galasoft.ch/>
>
> >> Web:http://www.galasoft.ch
>
> >> Support children in Calcutta:http://www.calcutta-espoir.ch
>
> >> [image: cid:image001....@01C9C8AA.B722DA80]
>
> >> My <http://galasoft.ch/contact/mobile/>**
>
> >> *business <http://galasoft.ch/contact/mobile/>*
>
> >> *card <http://galasoft.ch/contact/mobile/>*
>
> >> as
>
> >> Microsoft Tag <http://www.microsoft.com/tag>
>
> > --
> > Peter O'Hanlon
>
> --
> Sacha Barber
> sacha.bar...@gmail.com- Hide quoted text -
>
> - Show quoted text -

Sacha Barber

unread,
Nov 6, 2009, 9:28:00 AM11/6/09
to wpf-di...@googlegroups.com
I use attached DP. Mark does both.

Its all cool. Got to keep the fans happy man.
--
Sacha Barber
sacha....@gmail.com
Reply all
Reply to author
Forward
0 new messages