bluedog
bluedog

Reputation: 955

Bind VM Event to Command in XAML

The question is "How do I map an event triggered by the VM to a command using XAML?"

I'm new to WPF and MVVM. What I really want to do is this...

The UI has a button and a grid control. The user selects a single item in the grid control and presses the button. Depending on the type of item selected in the grid (A or B), one of two different modal dialogs are displayed to ask the user for extra input. When the user clicks OK on the dialog, the correct command is executed.

Here's how I think I'm supposed to do it...

The XAML binds the button to a command. That command looks at the currently selected item and raises one of two different events to indicate the type of item that is selected. In XAML these two events need to be linked to commands.

This brings me to the question that I asked above. If my VM fires two different events, how can I bind each of those to a command in XAML?

Upvotes: 0

Views: 405

Answers (2)

Peter
Peter

Reputation: 1687

Hello i created a sample for you - you didnt really provide any sample code so i just give an common example

public class MyViewModel : BaseViewModel
{

    private object selectedItem;
    private ICommand myCommand;


    public ICommand MyCommand
    {
        get
        {
            if(myCommand == null)
            {
                myCommand = new RelayCommand(MyCommandMethod, CanIExecuteMyCommand);
            }

            return myCommand;
        }
    }


    public object SelectedItem
    {
        get
        {
            return selectedItem;
        }
        set
        {
            selectedItem = value;
            RaisePropertyChanged("SelectedItem");
            CommandManager.InvalidateRequerySuggested();
        }
    }

    private void MyCommandMethod()
    {
        if(SelectedItem is MyClassA)
        {
            // do A stuff
        }
        else if(SelectedItem is MyClassB)
        {
            // do B stuff
        }

        //Can this happen?
    }

    private bool CanIExecuteMyCommand()
    {
        return selectedItem != null;
    }

}

RelayCommand watch here

What you wanna do is bind the SelectedItem to your GridView. So you can validate it in the ViewModel. You talked about Validation - you can do this by giving your Command a Condition like CanIExecuteMyCommand().

You should use a parent class for MyClassA and MyClassB! They should be - because you are using them in the same GridView. Else go with object mate.

Upvotes: 2

delagetto.so
delagetto.so

Reputation: 18

The extra information is extra information required for that command, so you should make it a continuation of the execution - no need for events yet.

Instead, invoke the IDialogViewModel's OpenDialog(new ExtraInfoViewModel(this->model)).

When a user clicks OK on the dialog, publish an UserUpdatedEvent from the ExtraInfoViewModel to your Mediator. Now you can simply your original VM subscribe to the UserUpdatedEvent message, and will execute the right command based on the user type info on the event message.

Upvotes: 0

Related Questions