Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

How to bind ToolBarItem to Command of ViewModel?

$
0
0

I tried to add this code to the view model:

    private Command<object> saveNoteCommand;

    public Command<object> SaveNoteCommand
    {
        get {
            return saveNoteCommand ?? (new Command<object> (ExecuteSaveCommand));
        }
    }

    private async void ExecuteSaveCommand(object parameter)
    {
        var tClient = new TicketClient ();
        var saveResponse=await tClient.SaveNoteAsync (_note);
        if (saveResponse.StatusCode == System.Net.HttpStatusCode.OK) {
            await _navigation.PopAsync ();
        }
    }

and this to the page:

this.BindingContext = new NoteDetailViewModel (tNote,this);

var btnSave = new ToolbarItem ();
btnSave.Name = "Save";
btnSave.SetBinding<NoteDetailViewModel> (ToolbarItem.CommandProperty, vm => vm.SaveNoteCommand);
this.ToolbarItems.Add (btnSave);

This code does, well, nothing. ;-) ExecuteSaveCommand is not fired when I click on the Toolbaritem. Any ideas what the problem is?


Viewing all articles
Browse latest Browse all 58056

Trending Articles