WPF Reflections

Oct 19 2007   1:29PM GMT

WPF command pattern part 2



Posted by: MarkWPF
Tags:
WPF

Following on from my previous post about WPF and the command pattern, how can we avoid the manual process of calling BindToElement from within code? 

What would be nice is if you could specify a commandbinding to point to a static instance of a command, but you can’t. 

But what you can do is create a attached property that will add the command binding for you.
Luckily, someone has been there before –  and a plaudit to Dan Crevier.
See his implementation at http://blogs.msdn.com/dancre/archive/2006/09/15/dm-v-vm-part-7-encapsulating-commands.aspx. How this works is by adding the binding into the code that implements the attached property.

That means your command definition in your xaml can be:

<Button Name=”blah” Command=”{Binding x:Static    gc:RoutedCommands.Exit}}” local:CreateCommandBinding.Command=”{Binding x:Static gc:RoutedCommands.Exit}}” >

And the command is bound to the button automatically, and we are singing and dancing

:-)

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: