WPF relaycommand from usercontrol

Posted by pilsdumps on Stack Overflow See other posts from Stack Overflow or by pilsdumps
Published on 2010-04-06T21:56:20Z Indexed on 2010/04/07 3:23 UTC
Read the original article Hit count: 821

Filed under:
|
|
|

Hi,

I'm new to WPF and in the spirit of trying to do things the correct way have tried to implement MVVM in my application. I've made use of the frequently mentioned article by Josh Smith, and apart from making me realise how little I know, it has left me a little stumped.

Specifically, I have a page that uses the RelayCommand object to handle a button directly on the page and this is fine. However, the button (save) will ultimately be on a user control that will also contain other buttons and the control will be used on a number of pages.

My question is this; how do I relay the command from the user control to the page (ie viewmodel) containing it? If I bind to the command

public ICommand SaveCommand
{
  get
  {
    if (_saveCommand == null)
    {
      _saveCommand = new RelayCommand(
          param => this.Save(),
          param => this.CanSave
          );
    }
    return _saveCommand;
  }
}

on the user control, I would need to use a Save method on the user control itself, when in fact I should be handling it on the viewmodel.

Can anyone help?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about relaycommand