0

I am trying to understand the MVVM pattern and so i started learning the concepts of binding and commands in C#. In order to understand this pattern i created a simple GUI for a proxy server. The GUI has two buttons, one for starting and one for stopping the server.

If a user starts the proxy by clicking on the start button i would like to disable the start button and enable the stop button. This way i can be sure that the stop proxy method can only be invoked when the proxy is running. When the user clicks on the stop proxy button i would like to enable the start proxy button again.

I order to realize this i created a viewmodel class that looks like this:

class ViewModelBase {
    public ICommand StartProxy { get; set; }
    public ICommand StopProxy { get; set; }
    public bool IsProxyRunning = false;

    public ViewModelBase() {
        StartProxy = new StartProxy(StartProxyMethod);
        StopProxy = new StopProxy(StopProxyMethod);
    }

    private void StartProxyMethod(object Parameter) {
        MessageBox.Show("Implement the start proxy method here");
        IsProxyRunning = true;
    }

    private void StopProxyMethod(object Parameter) {
        MessageBox.Show("Implement the stop proxy method here");
        IsProxyRunning = false;
    }
}

The code for the start and stop proxy commands is as following:

StartProxy

public class StartProxy : ICommand {
    Action<object> StartProxyMethod;
    public event EventHandler CanExecuteChanged;

    public StartProxy(Action<object> StartProxyMethod) {
        this.StartProxyMethod = StartProxyMethod;
    }

    public bool CanExecute(object Parameter) {
        //How to check if the proxy is not running (if so, this method can be executed)
        return true;
    }

    public void Execute(object Parameter) {
        StartProxyMethod(Parameter);
    }
}

StopProxy

public class StopProxy : ICommand {
    Action<object> StopProxyMethod;
    public event EventHandler CanExecuteChanged;

    public StopProxy(Action<object> StopProxyMethod) {
        this.StopProxyMethod = StopProxyMethod;
    }

    public bool CanExecute(object Parameter) {
        //How to check if the proxy is running (if so, this method can be executed)
        return true;
    }

    public void Execute(object Parameter) {
        StopProxyMethod(Parameter);
    }
}

Now im wondering how i can achieve this. I thought that i could pass the IsProxyRunning variable in the StartProxyMethod and the StopProxyMethod (in the viewmodel) and delegate the value of the variable to the buttons in the GUI. However this didn't work as expected.

I feel like i'm not looking in the right direction to archive my goal, if you have an idea where i'm wrong i would love to know.

2 Answers 2

1

you can use RelayCommand for the Command-Properties in the ViewModel. In the CanExecute-Method return !IsProxyRunning or without ! for stop.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, your suggestion pointed me in the right direction. However how can i toggle the button on runtime? In my viewmodelbase i define it as StartProxy = new StartProxy(o => { StartProxyMethod(); }, o => true); but when the proxy has started the button should be disabled (so o => true should become o => false)
in the viewmodelbase constructor you can use it´s Property IsProxyRunning, also in the funcs for canExecute.
I overlooked that part, thanks for your clarification. With your suggestion i managed to get it working and to get some understanding of the concepts. Thanks again!
0

You are right that you are doing it wrong.

You need to use this constructor for the Command (in the first part (Action) define the method that you execute, and in the Func define when the button can execute):

public Command (Action<object> execute, Func<object,bool> canExecute);

Then you need to remove the StartProxy.cs and StopProxy.cs.

Finally, when the button is pressed you need to call:

StartProxy.ChangeCanExecute();
StopProxy.ChangeCanExecute();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.