Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I'm meeting a problem when using command in MVVM model.
In my App, I have a button and I mst raise 2 event PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp for this button.
And I want to hook 2 above event into 2 command MouseLeftDownCommand and MouseLeftUpCommand in viewmodel class to handle.

So the question is: How to hook 2 command to 2 event of the same control ?

Please help me to resolve this problem.
Thanks and regards,
Posted
Updated 6-Nov-12 14:24pm
v4
Comments
Jim Jos 6-Nov-12 4:14am    
What is the code you have written? Could you put your code? I am not sure what exactly your problem is.. YOu need to use both down and up events? Are you trying to a drag and drop? Otherwise one event is more than necessary
tuandung2006 6-Nov-12 5:00am    
Thanks for your reply.
1. The code I have written is WPF.
2. In view class, I have a button, and when MouseDown event raise on this button, it will be binding to a "MouseDown" command(method) that is handle in viewmodel class. And when MouseUp event raise on this button, it will be binding to a "MouseUp"(method) command.
3. Are you trying to a drag and drop?: I just want to catch MouseDown and MouseUp event from view class and handle it inside viewmodel class.
4. Maybe we'll just need a command to handle all event, but at that time we must pass
argument to know which event is raised.
Jim Jos 6-Nov-12 7:11am    
OK.. You could have a single command handling both the events are you doing two differnt things in the mouse down and mouse up commands?

1 solution

Hi,

Within you viewmodel you'll have two commands:

C#
public ICommand MouseLeftDownCommand {get;set;}
public ICommand MouseLeftUpCommand {get;set;}


Obviously, you'll need to instantiate them and hook them up to some delegate command

Then, within you view (XAML page) you can do something like this:

XML
<button>
  <button.inputbindings>
    <mousebinding gesture="LeftDownClick" command="{Binding MouseLeftDownCommand }" />
    <mousebinding gesture="LeftUpClick" command="{Binding MouseLeftUpCommand }" />
</button.inputbindings>
</button>


Kind regards,
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900