Subscribe to an event from code behind in c#.net

c# wpf 5 years, 9 months ago
using System.Windows; using System.Windows.Input; //Use screenshot to get the xaml file namespace WpfApplication4 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //Here pnlMainGrid is the name of the grid coming from xaml design.Please make sure the same name suppose to be there. pnlMainGrid.MouseDown += new MouseButtonEventHandler(Grid_MouseDown); } private void panelMainGrid_MouseUp(object Sender, MouseButtonEventArgs e) { MessageBox.Show("You clicked me at " + e.GetPosition(this).ToString()); } private void Grid_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("You clicked me at " + e.GetPosition(this).ToString()); } } }
1940
Instructions

In this example i have written one mouse event directly from xaml and one from code behind.Any suggestion and comments, feel free to mention.Happy coding :-)

Posted By