for me this Command is bit confusing .I am implementing MVVM pattern in my project and often i need to use Command for exucuting methods in ViewModel . But sometimes i too need to know which actual event was raised in the View ?
e.g., there is a CheckBox control in View and Command property has been binded to a RelayCommand object in the ViewModel . Now , when the method is handled in ViewModel i need to know whether CheckBox's Checked event or UnChecked event was fired in the UI ?
Please if anybody can let me help clear my doubt . What is this Command actually used for ? There are already events like Checked , UnChecked , GotFocus , LostFocus , Click etc.... then why there was need for Command property ? Even if they have created what is speciality about that ! they are handled againt another event only .Like if CheckBox's Checked or UnChecked or Button's Click event is raised on UI then only Command property's underlying RelayCommand method is executed .
Anyway , going inside the RelayCommand's method is there any way to know which event was raised in the Client UI ?
Answers & Comments...Answer: 1 Hi,
Take a look at Command section. In Silverlight you'd better use DelegateCommand of Prism, It is more function than RelayCommand.
http://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx
Answer: 2 Yes, i have already read that quite long ago . Now what i need is , want to know which UI event was actually raised so that the Command object's method got executed .
<CheckBox Command="{Binding HandleThis}" />
Now ,lets see about this Checkbox . When HandleThis's underlaying method will execute ? Yea , you and no body is sure , it me be handled after CheckBox's Checked event , or it may be at CheckBox's UnChecked event ... or it may be bla..bla..etc...
So, is it possible to know which event was raised on UI , that triggered Command's underlaying method to exccute ?
Or my problem is not going to be clear ?
Answer: 3 jackWyu
jackWyu
There's no event was riased on UI except you use Trigger of Expression blend attach to Event.
Take a look at RelayCommand
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Event and ICommand are diffrent.
Answer: 4 thaicarrot
jackWyu
jackWyu
There's no event was riased on UI except you use Trigger of Expression blend attach to Event.
Take a look at RelayCommand
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Event and ICommand are diffrent.
That link i have also already gone through. And out of that i am in confusion about these Event and Command .
You said Event and ICommand are different . Yes they are different . Theoratically they are different , ICommand is an interface , Event is something like mechanism to handover some message to some other object to work another task. Yes these are theoratically and practically too well understood . But i am great confusion about the fact that how on Clicking on a Button , Command's underlaying method is called in the ViewModel code file ?
Command = "{Binding PropertyName}" . This PropertyName's underlayign object (RelayCommand Object) contains method , that is invoked . That's fine .It is invoked . But on the surface first of all there must have been Click event raised ! Since we have clicked on the Button , it must have raised Click event . But u are saying that ds'nt raises any event ? How it that going ? Hae , its going to be a black magic for me. All the resources link that u provided i have already gone through , and i have come up with confusions out of those documents . They are mine gererated . Anybody can help me here to get out ?
Answer: 5 jackWyu
jackWyu
It is riased click event.
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached( "Command", typeof(ICommand), typeof(ButtonBase), new PropertyMetadata(OnSetCommandCallback));
Answer: 6 thaicarrot
It is riased click event.
Yes, that i want to know in the method that is called in ViewModel . Is it possible ?
Which event was raised in the UI ? In case of button here it was click . Lets say there were a CheckBox , then it may be Click , Checked or UnChecked etc. that i want to know in the ViewModel's method , that which event was actually raised in the UI .
Answer: 7 jackWyu
jackWyu
It is depending on who implement those lagic, Inside the event they call the ICommand's ExecuteCommand(); then fire at viewmodel.
Answer: 8 If you do something like this:
<CheckBox Command="{Binding MyCommand}" IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
Then when the Command MyCommand is executed, the viewmodel will ALREADY have the IsChecked property set. Then in the command, you might want to take different actions based on the IsChecked property value of the viewmodel.
That would be a reason to have a Command on a checkbox. But there is no reason to need to know the event that I can think of.
Did you have something specific in mind?
Another possiblity, if you have multiple bindings to the command and you want to know which one it was, you could just use a CommandParameter to identify the source of the command.
Answer: 9 mtiede@swtechnologies.com
Another possiblity
Yea, you are somewhat closer to me . Let me simplify that.
Take simple CheckBox ,
<CheckBox Command="{Binding MyCommand}" />
When the Command MyCommand is executed ... please wait here .Just hold on.
Let me interrogate , when this MyCommand gets executed ? Please think here , don't be in hurry ,when ?
When you think you will realize where i was confused . It can be Checked or Unchecked any of the UI event . In both cases MyCommand gets executed .
This is my confusion . When i am in MyCommand , i want to know user raised which event on the screen ?
I don't think my question was that confusing , but i think you people did'nt read my problems carefully and unnecesseryly we wasted our time .
Anyway , can i hope now ?
Answer: 10 I'm not sure what you are asking. As I said, if you do this:
<CheckBox Command="{Binding MyCommand}" IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
The IsChecked bound property will be set first. So when the command is triggered, you can just look at your Viewmodel property to decide what the command should do based on the state of the property.
I see no reason to know which event is involved. Probably ever.
Maybe your problem would be clearer if you posted an example.
On re-reading your original post, I see you are also asking why did they make Checked, Unchecked AND Command. And the answer is historical. At first there was NO Commanding, at all. And with the advent of MVVM, commands were added. But the old Checked and Unchecked, for instance, are still there for backward compatibility.
But I wouldn't recommend doing anything other than my example above which should cover everything you need. Don't use the old style.
Answer: 11 Yea,
you reached there .
mtiede@swtechnologies.com
The IsChecked bound property will be set first.
This i was unknown . This was one of the loophole.
mtiede@swtechnologies.com
I see no reason to know which event is involved. Probably ever.
Now , to me also there seems no reason to know which event is involved . But still for my knowledge i want to know is that possible ?
Though presently it is of no use , but if we need can we know that ?
mtiede@swtechnologies.com
And the answer is historical. At first there was NO Commanding, at all. And with the advent of MVVM, commands were added.
Can you suggest some text books for this , that covers Silverlight 5 with MVVM .
Answer: 12 I don't think there is any way to know which event and the reason for that is that you should never need to know.
I don't know about books. I learned everything from the videos that are in the Learn tab at the top of the page. (Back then the videos were different ones and I think in some regards better than the current ones). And by reading a WPF book "Pro WPF in C# 2008".
The SL home page used to have links to books. Maybe it is still somewhere here on the site.