This is a migrated thread and some comments may be shown as answers.

MVVM and Commanding

7 Answers 120 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mike Kacos
Top achievements
Rank 1
Mike Kacos asked on 23 Feb 2011, 07:22 PM
Hi,
   I'm trying to find out if I can do what I'm hoping to do.  I've been looking at your demo for commanding,(http://demos.telerik.com/silverlight/#GridView/Commands), but I'm trying to do something a little different.  When I hit the Save Insert/Edit button (from the example) I want to fire off custom code in my ViewModel.  Now I like the behavior of the button in the example where they are enabled and disabled based on whether or not the user has begun editing etc... but I don't know how to then make my code fire.  

My example would be if I have a grid whose ItemSource is a collection of objects or type CARS.  I have a SelectedItem of type Car called SelectedCar.  I have a  SaveCommand in my ViewModel that performs an update on a SINGLE Car object.  IS there a way to achieve the desired results of enabling and disabling the button based on the Grid mode (i.e. editing) but then fire a command from my viewmodel?

Thanks in advance,

-Mike

7 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 01 Mar 2011, 11:40 AM
Hi Mike Kacos,

You may try using a custom button type that not only executes the command, but adds your custom code.
public class CustomButton : RadButton  
    {  
        public CustomButton()  
        {  
            Click += (sender, e) =>  
            {  
                if (Command == RadGridViewCommands.Delete &&  
RadGridViewCommands.Delete.CanExecute(null))  
    
                {  
                    RadGridViewCommands.Delete.Execute(null);  
                            // Your Code comes here. 
                }  
            };  
        }  
      
        public static DependencyProperty CommandProperty =  
            DependencyProperty.Register("Command",  
                                        typeof(ICommand), typeof(CustomButton),  
                                        new PropertyMetadata(null));  
      
        public ICommand Command  
        {  
            get { return GetValue(CommandProperty) as ICommand; }  
            set { SetValue(CommandProperty, value); }  
        }  
    


Regards,
Ivan Ivanov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Mike Kacos
Top achievements
Rank 1
answered on 02 Mar 2011, 03:27 PM
Thanks for the Reply.  I tried implementing this however, it does not seem to give me the functionality I want.  The button stays enabled no matter what.  The button I'm trying to implement is a "Save Insert/Edit" (Not Delete), but the button remains enabled the entire time.  I am looking to get the button to become enabled ONLY when the user clicks into a cell to begin editing and disabled once the user is finished editing.

-Mike
0
Ivan Ivanov
Telerik team
answered on 07 Mar 2011, 01:38 PM
Hi Mike Kacos,

Well, I have given you an example utilizing a random command. To be more specific to your case, I have prepared a sample project for you, which implements the same functionality of the "Save" button as in the demo. Please, refer to it and inform us if this solution fits into your requirements.


All the best,
Ivan Ivanov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Mike Kacos
Top achievements
Rank 1
answered on 08 Mar 2011, 05:17 PM
Thanks for taking the time to create the sample project for me.  Unfortunately the project doesn't seem to be working as I'd expect.  The enabling/disabling of the button works great, however the custom code (Specifically the "
 MessageBox.Show( "this value has been updated" );") never fires.  
0
Ivan Ivanov
Telerik team
answered on 08 Mar 2011, 06:17 PM
Hello Mike Kacos,

Well, in order to trigger the "custom code", you should press the button and invoke the command. Please, refer to this clip and let us know, if you could replicate the desired behavior:
Command button example


All the best,
Ivan Ivanov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Mike Kacos
Top achievements
Rank 1
answered on 08 Mar 2011, 07:01 PM
Thanks, I see that it does work from the clip, however it's not working for me locally from the project you sent.  I'll try to dig in a bit more and see if I can figure out what's going on.  Thanks.
0
Ivan Ivanov
Telerik team
answered on 09 Mar 2011, 10:59 AM
Hi Mike Kacos,

It is quite strange that you cannot replicate this. Could you please put a breakpoint in the event handler's block, in order to check if everything carries out correctly? 

Greetings,
Ivan Ivanov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
Mike Kacos
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Mike Kacos
Top achievements
Rank 1
Share this question
or