Reputation: 1011
my code in ViewModel :
public void Insert_Click(object sender, RoutedEventArgs e)
{
System.Windows.MessageBox.Show("Insert_Click");
}
code in View :
<Button Click="{Binding Insert_Click}" Background="Black" Height="56" Name="btnAdd" Width="57">
</Button>
error :
Error 1 Click="{Binding Insert_Click}" is not valid. '{Binding Insert_Click}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid
Please show me the correct code
Upvotes: 2
Views: 1068
Reputation: 10789
The event hook ups will only work for code behind to the control/window if you remove the Binding
syntax from the event handler. For MVVM it is a bit different. You can get that to work if you move the handler to the code behind but I suspect you want to use MVVM.
Here what you really need is to use the Command pattern
<Button Command="{Binding Insert}" Background="Black" Height="56" Name="btnAdd" Width="57"/>
and view model
public ViewModel()
{
Insert = new RelayCommand(Insert_Click);
}
public ICommand Insert { get; private set; }
private void Insert_Click()
{
System.Windows.MessageBox.Show("Insert_Click");
}
This is using a framework such as MVVM light
Upvotes: 5