[Solved] WPF MVVM pattern


Your question is a bit scattered. But I’ll try address what I think are your issues.

You say in the code behind you have:

this.DataContext = new CategoryViewModel();

But nothing else.

First thing to do with checking why your button isn’t working would be to see what action it is performing. Your XAML states it’s using a click event:

btnDeleteCategory_Click

Where’s that? Is it not in your code-behind too? It might be that you’ve not got anything and that’s why your button isn’t doing anything – you’ve not instructed it to do anything!

In MVVM you should be binding your button using Commands in your ViewModel, similarly to how you bind data to Properties in your ViewModel.

You need something like:

Command="{Binding Path=DeleteCommand}"

in your view, and:

public ICommand DeleteCommand
{
    get { return new DelegateCommand<object>(FuncToCall, FuncToEvaluate); }
}

private void FuncToCall(object context)
{
    //this is called when the button is clicked - Delete something
}

private bool FuncToEvaluate(object context)
{
    //this is called to evaluate whether FuncToCall can be called
    //for example you can return true or false based on some validation logic
    return true;
}

Binding to TxtName might not be working because it does not implement/call PropertyChanged.

solved WPF MVVM pattern