Binding two commands to one button?

前端 未结 2 544
南笙
南笙 2020-12-06 13:28

I have a tab control contains two tabs, one shows me the messages of a running process and the other shows me a web page!

I have three buttons (start, stop and clear

相关标签:
2条回答
  • 2020-12-06 13:54

    You want to click one Button and run two pieces of code... it really doesn't sound that complicated. Using one of the available RelayCommands, the problem could be fixed as simply as this:

    public ICommand SomeCommand
    {
        get { return new ActionCommand(action) => { RunFirstFunction(action); 
            RunSecondFunction(action) }, canExecute => someCondition); }
    }
    

    In XAML:

    <Button Command="{Binding SomeCommand}" CommandParameter="{Binding SomeObject}">
        Click Me
    </Button>
    

    ActionCommand is my own form of the RelayCommand, but all of the delegate ICommand implementations are roughly equal and could work in this way. The CommandParameter value comes into the code as the action variable and is passed through to the two functions.

    0 讨论(0)
  • 2020-12-06 14:03

    You could use the CompositeCommand from the Prism Framework. Create an additional CompositeCommand in your viewmodel, register both ordinary commands with this CompositeCommand and bind the button to the CompositeCommand.

    See the Prism documentation on page 130 on how to work with CompositeCommands.

    Your app needs to reference Microsoft.Practices.Prism.dll which is included in the Prism package.

    0 讨论(0)
提交回复
热议问题