I’m reviewing some courseware and I’d like to update the M-V-VM part.
What I’m looking for is a way to easily bind routedcommands exposed by viewmodel inside the view.
This is the cleanest solution I’ve found:
http://blogs.msdn.com/dancre/archive/2006/09/15/dm-v-vm-part-7-encapsulating-commands.aspx but it doesn’t work with multiple commands.
What I’d like to have is something like
<Grid>
<l:CommandBinder>
<CommandBinder BindTo:{AddCommand} />
<CommandBinder BindTo:{RemoveCommand} />
</l:CommandBinder>
</Grid>
Where Add/RemoveCommand are routed command exposed from associated DataContext.
Anyone has an idea of how to bind a command w/o using CommandBinding object?
Corrado
Tested it and everything works fine, apart the previously mentioned problem…
Corrado
<br
Hi Josh,
I’d like the idea of using RoutedCommands instead of ICommand so that I could handle commands coming from different sources instead of wiring each command straight to the command and I’m looking for a clean solution (if any) that allows me to do that w/o using CommandBinding but just specifying the command name as Dan’s sample does.
What’s your approach if you need to handle the same command coming from different sources?
Corrado
War is peace. Freedom is slavery. Bugs are features.
Bill summarized my idea, what I don’t really like in Bill’s snippet:
<Window mvp:Presenter.Instance="{x:Type SomePresenter}">
<mvp:Presenter.ActionBindings>
<mvp:ActionBindings>
<mvp:CommandActionBinding Command="ApplicationCommands.Close"
Executed="OnClose"
CanExecute="CanClose"/>
</mvp:ActionBindings>
</mvp:Presenter.ActionBindings>
</Window>
Is the presence of delegate method names inside the Xaml
I really love to have something like (in pseudoXaml)
<Window>
<x:CommandBinder>
<CommandBind BindTo:{Commands.Open} />
<CommandBind BindTo:{Commands.Open} />
</x:CommandBinder>
<Window>
Hope your clever minds could make me say “Yess!” J
Josh,
ICommand (the one you use in your great article J fit my needs) but I also love RoutedCommand so I’d like to expose RoutedCommands from ViewModel and wire them up via Xaml in the cleanes mode as possible (see my previous pseudo-sample)
Basically, I like Bill’s solution apart comment I exposed in my previous post, don’t know If my idea it’s applicable but that’s what I’d love to see.
Oops sorry, I meant:
<Window>
<x:CommandBinder>
<CommandBind BindTo:{Commands.Open} />
<CommandBind BindTo:{Commands.Close} />
</x:CommandBinder>
<Grid>
<Button Command={Binding Commands.Open} />
<Button Command={Binding Commands.Close} />
</Grid>
<Window>
In short: I’d like to have the CommandBinding behavior but w/o specifying method names (wiring should happen inside viewmodel in the same way exposed by Dan’s code) but I have absolutely idea if this is possible.
I agree with your ideas, InputGesture and routing to different presenter are ideas I’ve considerated.
Corrado
Josh,From my point of view, and some of the user stories behind why I'm designing the ActionBindings:* Depending on the nature of how the command was initiated, we may want to bind to different handlers. For instance, a DeleteCommand may be used on two different buttons, meant to remove items from two different lists. The Presenter should be ignorant of the View, so shouldn't know details about which button is clicked or if any button is clicked to initiate the command. We could use multiple commands here, but that seems like overkill in this simple case, and may not even be possible in others (wanting to handle standard commands, for instance). Of course, this point doesn't seem to apply to Corrado's scenario, since he explicitly doesn't want to specify the handler.
* We want to be able to bind commands we don't have control over the definition of. We can't make the command a DelegatingCommand concept, because the command was already defined as a Routed(UI)Command. I have no idea if this applies to Corrado.
* We may actually be routing to different Presenters in a composite application. This is very similar to the first point, but instead of different handlers, we have entirely different target objects.
* Routed(UI)Commands give us more functionality than just the routing. The InputGestures, Name and Text are all handy features to lose.
See my comments inline...
On Thu, Jul 24, 2008 at 4:44 PM, Bill Kempf <wek...@gmail.com> wrote:
Josh,From my point of view, and some of the user stories behind why I'm designing the ActionBindings:* Depending on the nature of how the command was initiated, we may want to bind to different handlers. For instance, a DeleteCommand may be used on two different buttons, meant to remove items from two different lists. The Presenter should be ignorant of the View, so shouldn't know details about which button is clicked or if any button is clicked to initiate the command. We could use multiple commands here, but that seems like overkill in this simple case, and may not even be possible in others (wanting to handle standard commands, for instance). Of course, this point doesn't seem to apply to Corrado's scenario, since he explicitly doesn't want to specify the handler.
[JAS] - According to my worldview, in the Delete scenario you described, I would have two instances of the Delete command (one class, two instances) exposed as properties of my ViewModel. I would provide each instance with a delegate to invoke upon execution, in an IoC mode of thinking.
* We want to be able to bind commands we don't have control over the definition of. We can't make the command a DelegatingCommand concept, because the command was already defined as a Routed(UI)Command. I have no idea if this applies to Corrado.
[JAS] - No comment.
* We may actually be routing to different Presenters in a composite application. This is very similar to the first point, but instead of different handlers, we have entirely different target objects.
[JAS] - Since, in my approach, every ViewModel object exposes its own private instance of a command class, that is not an issue.* Routed(UI)Commands give us more functionality than just the routing. The InputGestures, Name and Text are all handy features to lose.
[JAS] - That's a good point. I can see the appeal of keeping gestures and localized display text around. However, I should point out, input gestures are not exclusive to routed commands. They work with any ICommand object. And, of course, it's not too difficult to add a Text property to a custom command base class.
Josh,
You’re my hero!
I’ll have a look at it asap J
Amazing
Corrado
From: wpf-di...@googlegroups.com
[mailto:wpf-di...@googlegroups.com] On Behalf Of Josh Smith
Sent: venerdì 25 luglio 2008 06:02
To: wpf-di...@googlegroups.com
Subject: Re: Binding Commands in M-V-VM
My brain caught on fire tonight
and I worked for seven straight hours on an implementation for what Corrado
described (or something like it). I also published an article about it,
here: http://www.codeproject.com/KB/WPF/VMCommanding.aspx
Josh
Josh,
From my point of view, and some of the user stories behind why I'm designing the ActionBindings:
* Depending on the nature of how the command was initiated, we may want to bind to different handlers. For instance, a DeleteCommand may be used on two different buttons, meant to remove items from two different lists. The Presenter should be ignorant of the View, so shouldn't know details about which button is clicked or if any button is clicked to initiate the command. We could use multiple commands here, but that seems like overkill in this simple case, and may not even be possible in others (wanting to handle standard commands, for instance). Of course, this point doesn't seem to apply to Corrado's scenario, since he explicitly doesn't want to specify the handler.
I don't know what specific reason(s) Corrado has. They may even be reason(s) I don't list
Josh,
From my point of view, and some of the user stories behind why I'm designing the ActionBindings:
* Depending on the nature of how the command was initiated, we may want to bind to different handlers. For instance, a DeleteCommand may be used on two different buttons, meant to remove items from two different lists. The Presenter should be ignorant of the View, so shouldn't know details about which button is clicked or if any button is clicked to initiate the command. We could use multiple commands here, but that seems like overkill in this simple case, and may not even be possible in others (wanting to handle standard commands, for instance). Of course, this point doesn't seem to apply to Corrado's scenario, since he explicitly doesn't want to specify the handler.If you want to have the command act on a different List based on which button you pressed, you send the list as the command parameter...are we forgetting that this exists? I don't think routed commands are necessary at all.
* We want to be able to bind commands we don't have control over the definition of. We can't make the command a DelegatingCommand concept, because the command was already defined as a Routed(UI)Command. I have no idea if this applies to Corrado.
In this case we expose a Static ICommand object that was implemented somewhere else. How does a routed command help anymore in this situation.
* We may actually be routing to different Presenters in a composite application. This is very similar to the first point, but instead of different handlers, we have entirely different target objects.
In that case the Presenter you're binding to should expose the command
* Routed(UI)Commands give us more functionality than just the routing. The InputGestures, Name and Text are all handy features to lose.I don't know what specific reason(s) Corrado has. They may even be reason(s) I don't listBill, we discussed how to do all of those on Brownie Points so you don't lose any of the RoutedUICommand benefits. I think RUICommands are a necessity to address the needs of a non MVC/P/Whatever app. I still stand unconvinced that they provide anything above and beyond what can be done (easier) with generic ICommands.
On Fri, Jul 25, 2008 at 1:38 AM, Mike Brown <mbro...@gmail.com> wrote:
Josh,
From my point of view, and some of the user stories behind why I'm designing the ActionBindings:
* Depending on the nature of how the command was initiated, we may want to bind to different handlers. For instance, a DeleteCommand may be used on two different buttons, meant to remove items from two different lists. The Presenter should be ignorant of the View, so shouldn't know details about which button is clicked or if any button is clicked to initiate the command. We could use multiple commands here, but that seems like overkill in this simple case, and may not even be possible in others (wanting to handle standard commands, for instance). Of course, this point doesn't seem to apply to Corrado's scenario, since he explicitly doesn't want to specify the handler.If you want to have the command act on a different List based on which button you pressed, you send the list as the command parameter...are we forgetting that this exists? I don't think routed commands are necessary at all.[WEK] - No, I did not forge the parameter. However, that approach can be complicated. There's only one parameter. If I need that parameter for other information, then the only alternative with this approach is to use a compound parameter. However, compound parameters are extremely tricky to use declaratively. Not that I'm saying you can't, just that it becomes an overly complex alternative.
* We want to be able to bind commands we don't have control over the definition of. We can't make the command a DelegatingCommand concept, because the command was already defined as a Routed(UI)Command. I have no idea if this applies to Corrado.
In this case we expose a Static ICommand object that was implemented somewhere else. How does a routed command help anymore in this situation.[WEK] - That Static ICommand object happens to be a RoutedUICommand, and there's nothing you can do about it, because you don't have control over the command's definition.
* We may actually be routing to different Presenters in a composite application. This is very similar to the first point, but instead of different handlers, we have entirely different target objects.
In that case the Presenter you're binding to should expose the command[WEK] - This relies on using a different command, which I pointed out isn't always desirable, much less possible.
* Routed(UI)Commands give us more functionality than just the routing. The InputGestures, Name and Text are all handy features to lose.I don't know what specific reason(s) Corrado has. They may even be reason(s) I don't listBill, we discussed how to do all of those on Brownie Points so you don't lose any of the RoutedUICommand benefits. I think RUICommands are a necessity to address the needs of a non MVC/P/Whatever app. I still stand unconvinced that they provide anything above and beyond what can be done (easier) with generic ICommands.[WEK] - I think you and I disagree on the "easier" part of this argument. I think I've laid out fairly clear areas in which the DelegatingCommand concept is NOT easier to use.I believe that if Microsoft had provided some way to specify a target object when declaring event handlers in XAML, none of this would even be discussed. We'd all be doing something like this, and the DelegatingCommand never would have been born:<Window><Window.CommandBindings><CommandBinding Command="foo:MyCommands.FooCommand" Executed="{Event Target={StaticResource MyPresenter}, Handler=OnFooCommand}"/></Window.CommandBindings></Window>I'm just trying to get as close to what I think we should have had to begin with, with what can be currently implemented.
On Fri, Jul 25, 2008 at 10:06 AM, Bill Kempf <wek...@gmail.com> wrote:
On Fri, Jul 25, 2008 at 1:38 AM, Mike Brown <mbro...@gmail.com> wrote:
Josh,
From my point of view, and some of the user stories behind why I'm designing the ActionBindings:
* Depending on the nature of how the command was initiated, we may want to bind to different handlers. For instance, a DeleteCommand may be used on two different buttons, meant to remove items from two different lists. The Presenter should be ignorant of the View, so shouldn't know details about which button is clicked or if any button is clicked to initiate the command. We could use multiple commands here, but that seems like overkill in this simple case, and may not even be possible in others (wanting to handle standard commands, for instance). Of course, this point doesn't seem to apply to Corrado's scenario, since he explicitly doesn't want to specify the handler.If you want to have the command act on a different List based on which button you pressed, you send the list as the command parameter...are we forgetting that this exists? I don't think routed commands are necessary at all.[WEK] - No, I did not forge the parameter. However, that approach can be complicated. There's only one parameter. If I need that parameter for other information, then the only alternative with this approach is to use a compound parameter. However, compound parameters are extremely tricky to use declaratively. Not that I'm saying you can't, just that it becomes an overly complex alternative.I'm going off the specific example you provided, if you can give me an example where that isn't a viable solution, I can change my opinion. As of now, I'm not seeing it.
* We want to be able to bind commands we don't have control over the definition of. We can't make the command a DelegatingCommand concept, because the command was already defined as a Routed(UI)Command. I have no idea if this applies to Corrado.
In this case we expose a Static ICommand object that was implemented somewhere else. How does a routed command help anymore in this situation.[WEK] - That Static ICommand object happens to be a RoutedUICommand, and there's nothing you can do about it, because you don't have control over the command's definition.Are you talking about commands like App.Open or whatever? Those are for convenience. There's nothing preventing you from creating your own non-routed versions of those same commands.
* We may actually be routing to different Presenters in a composite application. This is very similar to the first point, but instead of different handlers, we have entirely different target objects.
In that case the Presenter you're binding to should expose the command[WEK] - This relies on using a different command, which I pointed out isn't always desirable, much less possible.No same command, just on a different presenter.
* Routed(UI)Commands give us more functionality than just the routing. The InputGestures, Name and Text are all handy features to lose.I don't know what specific reason(s) Corrado has. They may even be reason(s) I don't listBill, we discussed how to do all of those on B