MainPage.xaml , I bind the UserControl login using the namespace : xmlns: UC = "clr-namespace: Test.Views", since I have usercontrol in the folder named " Views ".
<ScrollViewer> <UC:Login BtnLoginClick="Login_BtnLoginClick"/> </ScrollViewer>
Login.cs
public partial class Login : UserControl { public event EventHandler BtnLoginClick; public Login() { InitializeComponent(); } private void btnLogin_Click(object sender, RoutedEventArgs e) { string userName = txtUserName.Text; string userPassword = txtUserPassword.Password.Trim(); if (userName != null && userName != string.Empty && userPassword != null && userPassword != string.Empty) { if (this.BtnLoginClick != null) { this.BtnLoginClick(this, e); } } else { MessageBox.Show("Invalid username or password"); } }
}
Finally, remember to use the event handler in MainPage.xaml to grab the held event from the User User to perform other actions.
MainPage.xaml
<UC:Login BtnLoginClick="Login_BtnLoginClick"/>
Here "BtnLoginClick" is the event handler defined in Login.xaml [User Control].
Create a new event for this "BtnLoginClick" event when I created "Login_BtnLoginClick".
MainPage.cs
private void Login_BtnLoginClick(object sender, EventArgs e) { Messagebox.Show("Event captured successfully");
source share