I have a user control with two buttons (Add, Delete). When accessed from the main window, how do I know which button were being clicked?
btnAdd
had a method while btnDelete
has another method that should be invoked.
I have a user control with two buttons (Add, Delete). When accessed from the main window, how do I know which button were being clicked?
btnAdd
had a method while btnDelete
has another method that should be invoked.
Create two separate Command for button add
and delete
in MainWindow
And you can bind your command in userControl like this:
<Button Command={Binding AddCommand}/>
and set the datacontext
of Mainwindow
to itself inside XAML or in a constructer
this.datacontext = this;
For more information on how to create Command see this and this
If I understand your issue correctly, you could cast the sender argument in the event handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
if (button.Name == "btnAdd ")
{
//add button was clicked...
}
else if (button.Name == "btnDelete")
{
}
}
<Button x:Name="btnAdd" Content="Add" Click="Button_Click" />
<Button x:Name="btnDelete" Content="Delete" Click="Button_Click" />