문제

My Viewmodel has an event

public class TestViewModel 
{
    public event RoutedEventHandler Run;
}

I wanna trigger this event when user clicked on a button in view

How to bind this with a button in a view ?

도움이 되었습니까?

해결책

Routed events are meant for controls not view models, if you have something that should be executed upon a button click a command would be more suitable in my opinion, it can easily be bound to the Button.Command.

다른 팁

I think, you should use DelegateCommand for such behavior.

In your View Model:

private DelegateCommand _runCommand;
public DelegateCommand RunCommand
{
    get
    {
        if (_runCommand == null)
            _runCommand = new DelegateCommand(Run, CanRun);

        return _runCommand;
    }
}

void Run()
{
    ... 
}

bool CanSaveAction()
{
    return true;
}

On your page:

<Button Command="{Binding RunCommand}" />
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top