public partial class MainPage : UserControl
{
private ListBox lb;
public MainPage()
{
InitializeComponent();
Button btn = new Button();
btn.Content = "Press Me!";
// btn.Click is a routed event
btn.Click += new RoutedEventHandler(btn_Click);
// btn.MouseLeftButtonUp is not a routed event, so attach
// to it via the Button.MouseLeftButtonUpEvent instead
btn.AddHandler(Button.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(btn_MouseLeftButtonUp),
true);
// btn.MouseLeftButtonDown is not a routed event, so attach
// to it via the Button.MouseLeftButtonDownEvent instead
btn.AddHandler( Button.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(btn_MouseLeftButtonDown),
true);
lb = new ListBox();
this.LayoutRoot.Children.Add(btn);
this.LayoutRoot.Children.Add(lb);
}
void btn_Click(object sender, RoutedEventArgs e)
{
lb.Items.Add("btn_Click");
}
void btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
lb.Items.Add("btn_MouseLeftButtonDown");
}
void btn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
lb.Items.Add("btn_MouseLeftButtonUp");
}
}
|