views:

51

answers:

1

I have a Silverlight page that has few controls and all of the behaviors are not databound. For example, RichTextBox is databound for the Text property. But the controls behaviors like Bold, Italic, Numbered, etc are driven from a button click event from toolbar above the control. These controls do change the value of the Data, but do not actually save/retrieve the data. So, is it ok to keep such UI events in code-behind page of the Silverlight page than creating a ICommand object in ViewModel?

MVVM says your View should interact with ViewModel for interacting with Model internally. Here the View is just updating the View and not the Model. So is Code-Behind for such events is fine?

+1  A: 

This kind of thing can be quite subjective, but remember that the ViewModel is the model of the view including settings for how the view is displayed. I have my buttons etc connect to ICommands in the ViewModel (google for 'DelegateCommand' or 'RelayCommand' for examples), not commands in the View. My ViewModel then updates its state as necessary and the View responds to events. Button clicks loop through the ViewModel, even if the VM does nothing other than fire events for the View to respond to (and handle exceptions and logging etc for the event).

For complex behaviour in the view which does not affect the data that it works with then of course handling events in the code-behind is appropriate, though I usually find this is the case in user controls, not windows etc.

AndrewS